Selenium Webdriver Python Click Doesn't Seem to be working - python

I'm trying to login to my Wegmans account to export my orders into a spreadsheet. I'm using Selenium and chromedriver in Docker. My issue is that clicking the next button on the login/sign in page isn't having any effect on the page.
Here's my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
print("Waiting for Webdriver to be available")
time.sleep(5)
print("Done waiting")
driver = webdriver.Remote(
command_executor='http://chrome:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME)
wait = WebDriverWait(driver, 20)
driver.maximize_window()
print("Opening Wegmans")
driver.get("https://shop.wegmans.com/login")
wait.until(EC.title_contains('Sign in'))
email = driver.find_element_by_id("signInName")
password = driver.find_element_by_id("password")
email.send_keys("myemail#yahoo.com")
password.send_keys("password")
driver.find_element_by_id("next").click()
driver.save_screenshot("/tmp/app/rightafterclick.png")
time.sleep(20)
driver.save_screenshot("/tmp/app/20secondsafterclick.png")
Both screenshots show the same thing - which is the email and password filled out but no change in the page. The 2nd screenshot should contain an error message because the email is not valid. The element id's are correct. How can I ensure that the "Sign In" button gets clicked?

I had solved your problem by using below code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument('--disable-blink-features=AutomationControlled')
option.add_argument("start-maximized")
option.add_experimental_option(
"excludeSwitches", ["enable-automation"])
option.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=option)
wait = WebDriverWait(driver, 20)
driver.maximize_window()
print("Opening Wegmans")
driver.get("https://shop.wegmans.com/login")
wait.until(EC.title_contains('Sign in'))
time.sleep(5)
email = driver.find_element_by_id("signInName")
password = driver.find_element_by_id("password")
email.send_keys("myemail#yahoo.com")
password.send_keys("password")
driver.find_element_by_id("next").click()
driver.save_screenshot("rightafterclick.png")
time.sleep(20)
driver.save_screenshot("20secondsafterclick.png")

Related

Python selenium Element error event wait function is not helping

This is my first attempt to login a website using selenium, i have written the below piece of code but not able to login the website, getting the following error message.
Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="txtUsername"]"}
(Session info: chrome=108.0.5359.95)
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
s=Service("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver = webdriver.Chrome(service=s)
driver.maximize_window()
driver.get("https://opensource-demo.orangehrmlive.com")
wait = WebDriverWait(driver, 10)
driver.find_element(By.NAME,"txtUsername").send_keys("Admin")
driver.find_element(By.ID, "txtPassword").send_keys("admin123")
driver.quit()
Looking for some suggestions
There are 2 problems with your code:
You created the wait object but not used it...
Your locators are wrong.
The following code works:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
wait = WebDriverWait(driver, 20)
url = "https://opensource-demo.orangehrmlive.com"
driver.get(url)
wait.until(EC.element_to_be_clickable((By.NAME, "username"))).send_keys("Admin")
wait.until(EC.element_to_be_clickable((By.NAME, "password"))).send_keys("admin123")
wait.until(EC.element_to_be_clickable((By.TAG_NAME, "button"))).click()
The wait needs to be called:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
s=Service("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver = webdriver.Chrome(service=s)
driver.maximize_window()
driver.get("https://opensource-demo.orangehrmlive.com")
wait = WebDriverWait(driver, 10)
user = wait.until(EC.element_to_be_clickable((By.XPATH, "//div")))
user.send_keys("Admin")
driver.find_element(By.ID, "txtPassword").send_keys("admin123")
driver.quit()

Selenium python driver doesn't click or press the key for the button all the times

I'm using selenium to get to YouTube and write something on the search bar and then press the button or press the enter key.
Both clicking or pressing a key does sometimes work, but sometimes it does not.
I tried to wait with WebDriverWait, and I even changed the waiting time from 10 to 20 seconds, but it didn't make any difference.
And if I add anything (like printing the new page title), it only shows me the first page title and not the title after the search.
Here is my code and what I tried:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def get_driver():
firefox_options = Options()
# firefox_options.add_argument("--headless")
driver = webdriver.Firefox(executable_path=r"C:\Program Files\Mozilla Firefox\geckodriver.exe", options=firefox_options)
driver.implicitly_wait(9)
return driver
driver = get_driver()
driver.get('https://www.youtube.com/')
search = driver.find_element(By.XPATH, '//input[#id="search"]')
search.send_keys("python")
# search.send_keys(Keys.ENTER) #using the enter key # If I add nothing after this line it work
# searchbutton = driver.find_element(By.XPATH,'//*[#id="search-icon-legacy"]') # This also dose doesn't work
# searchbutton.click() # using the click method() #also dose not work
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="search-icon-legacy"]'))).click() # Sometimes work
# driver.implicitly_wait(10)
# print(driver.title) # This show me only the title of the first page not the one after the search
Is it because I use the Firefox webdriver (should I change to Chrome)?
Or is it because of my internet connection?
To make this working you need to click the search field input first, then add a short delay and then send the Keys.ENTER or click search-icon-legacy element.
So, this is not your fault, this is how YouTube webpage works. You may even call it a kind of bug. But since this webpage it built for human users it works good since human will never click on the input field and insert the search value there within zero time.
Anyway, the 2 following codes are working:
First.
import time
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
options.add_argument('--disable-notifications')
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://www.youtube.com/"
driver.get(url)
search = wait.until(EC.element_to_be_clickable((By.XPATH, '//input[#id="search"]')))
search.click()
time.sleep(0.2)
search.send_keys("python")
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="search-icon-legacy"]'))).click()
Second.
import time
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
options.add_argument('--disable-notifications')
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://www.youtube.com/"
driver.get(url)
search = wait.until(EC.element_to_be_clickable((By.XPATH, '//input[#id="search"]')))
search.click()
time.sleep(0.2)
search.send_keys("python" + Keys.ENTER)

Accept or reject cookies with Selenium

I am trying to login to my Garmin connect account. I can enter the website by cannot get rid of the window to accept or reject cookies. Would you have a solution for this ? Here is my code:
Thanks a lot!
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
options = webdriver.ChromeOptions()
options.add_experimental_option("prefs", {"profile.default_content_setting_values.cookies": 1})
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)
driver.get("https://connect.garmin.com/signin/") #loading page
wait = WebDriverWait(driver, 20) #defining webdriver wait
#defining username and password
username = 'xxxxx'
password = 'xxxxx\n' #\n will act as enter key
def login():
wait.until(EC.visibility_of_element_located((By.XPATH, "//iframe[#id='gauth-widget-frame-gauth-widget']")))
frameLogin = driver.find_element(By.XPATH, "//iframe[#id='gauth-widget-frame-gauth-widget']")
driver.switch_to.frame(frameLogin)
wait.until(EC.visibility_of_element_located((By.ID, 'username'))).send_keys(username) #userame/email
wait.until(EC.visibility_of_element_located((By.ID, 'password'))).send_keys(password) #password
#try to click on reject cookies
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[#class='truste_popframe']")))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[#class='gwt-Frame']")))
frameCookies = driver.find_element_by_xpath("//iframe[#class='gwt-Frame']")
driver.switch_to.frame(frameCookies)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[#class='acceptAllButtonLower']"))).click()
login()
You can try to close the popup window, get the xpath of the close button and if it's regular and appears always at loading of page you can close it.

How do I make send keys in Selenium work?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = 'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get('https://outlook.office.com/mail/')
driver.implicitly_wait(7)
login = driver.find_element_by_name("loginfmt")
login.send_keys("emailhere")
login.send_keys(Keys.RETURN)
driver.implicitly_wait(5)
password = driver.find_element_by_name("passwd")
password.send_keys("passwordhere")
password.send_keys(Keys.RETURN)
It perfectly inputs the email and takes me to the password input screen, but at that point, it does not send the keys for the password. Any ideas on what is wrong with my code?
explicit wait is needed here. refer wait, so code will be like
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
password = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.NAME, "passwd"))
)
password.send_keys("passwordhere")
additional reference at: documentation
I have tried with below code, which is maximizing the screen, and then using Name attribute with Explicit waits, it is working fine.
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.get('https://outlook.office.com/mail/')
wait = WebDriverWait(driver, 20)
login_btn = wait.until(EC.element_to_be_clickable((By.NAME, "loginfmt")))
login_btn.send_keys("some name")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[value='Next']"))).click()
password_btn = wait.until(EC.element_to_be_clickable((By.NAME, "passwd")))
password_btn.send_keys("Your password")
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Python Automatic browser without webdriver

I have this code , it opens chrome, but it doesn't want to continue with the code. Don't really know how to fix the issue. I do NOT want it to open selenium webdriver, want it to open my own local chrome path, I want at the same time to make the script to read read elements and print the values.
import names, time, random
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
def AccGen():
while True:
# *************Static***************
prefs = {"profile.managed_default_content_settings.images": 1}
options = Options()
# options.add_argument('--disable-gpu')
# options.add_argument("--disable-extensions")
# options.add_argument('--disable-notifications')
options.add_experimental_option("prefs", prefs)
options.add_argument("--window-size=1600,900")
browser = webdriver.Chrome(executable_path='C:/Users/Jonathan/AppData/Local/Google/Chrome/Application/Chrome.exe',chrome_options=options)
browser.implicitly_wait(10)
# ------------------------------------
# Access to site
browser.get(
"https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ltmpl=default"
)
###################################################################
firstName = names.get_first_name()
lastName = names.get_last_name()
email = '{}.{}{}'.format(firstName, lastName, random.randint(1000, 9999))
password = '2001jl00'
###################################################################
# Write in random Name
WebDriverWait(browser, 20).until(
EC.visibility_of_element_located(
(By.XPATH, '//*[#id="firstName"]'))).send_keys(firstName)
https://mystb.in/vevivuneku.coffeescript

Categories

Resources