7

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?

1
  • 1
    It cannot be NoSuchElementException. In case of WebdriverWait + ExpectedConditions you should get TimeOutException! Are you sure that you show us correct code? Commented Jan 8, 2017 at 8:52

2 Answers 2

8

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()
Sign up to request clarification or add additional context in comments.

Comments

5

You don't need to use browser.find_element inside expected_conditions (that is why you get NoSuchElementException instead of TimeOutException). The correct way to use it is (By.ID, "id")

passwordElem = WebDriverWait(browser, 5).until(
    EC.presence_of_element_located((By.ID, "Passwd"))
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.