Issue
I try to log in to Google with Selenium. It works if I use sleep() like this code:
browser = webdriver.Firefox()
browser.get('https://admin.google.com/')
emailElem = browser.find_element_by_id('Email')
emailElem.send_keys('mymail')
nextButton = browser.find_element_by_id('next')
nextButton.click()
sleep(5)
passwordElem = browser.find_element_by_id('Passwd')
passwordElem.send_keys('mypass')
signinButton = browser.find_element_by_id('signIn')
signinButton.click()
If I change sleep to WebDriverWait like this
browser = webdriver.Firefox()
browser.get('https://admin.google.com/')
emailElem = browser.find_element_by_id('Email')
emailElem.send_keys('mymail')
nextButton = browser.find_element_by_id('next')
nextButton.click()
passwordElem = WebDriverWait(browser, 5).until(
EC.presence_of_element_located(browser.find_element_by_id('Passwd'))
)
passwordElem.send_keys('mypass')
signinButton = browser.find_element_by_id('signIn')
signinButton.click()
It shows an error like this:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="Passwd"]
How can I use WebDriverWait?
Solution
Yes, as Guy said, your browser.find_element_by_id('Passwd'))
is not neccesary. Change to((By.ID, "Passwd")))
as shown in the documentation. Here's what the code should be:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Firefox()
browser.get('https://admin.google.com/')
emailElem = browser.find_element_by_id('[email protected]')
emailElem.send_keys('youremail')
nextButton = browser.find_element_by_id('next')
nextButton.click()
passwordElem = WebDriverWait(browser, 5).until(EC.presence_of_element_located((By.ID, "Passwd")))
passwordElem.send_keys('yourpassword')
signinButton = browser.find_element_by_id('signIn')
signinButton.click()
Answered By - Vagif
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.