0

I am trying to automatically login the website using selenium with python. However, I got error as below.

Traceback (most recent call last):
  File "C:\Users\KienThong\Automation\Learning\GmailDemo.py", line 15, in <module>
    ID = WebDriverWait(driver, 10).until(
  File "C:\Python385\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

My code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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

driver = webdriver.Chrome()
driver.get("https://www.englishforum.com/study/login/")
driver.maximize_window()
try:
    ID = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "_xfUid-1-1643985254"))
    )
    ID.send_keys("[email protected]")
finally:
    driver.quit()
2
  • Which element is that in the UI? Commented Feb 4, 2022 at 15:03
  • It's the Email input bar, I am trying to fill in the Email input bar Commented Feb 4, 2022 at 15:51

3 Answers 3

1

The reason why you are having an error is that the ID that you are using is dynamic and changes everytime the page is loaded. Therefore we need to use a static identifier which we can with name="login"

driver = webdriver.Chrome()
driver.get("https://www.englishforum.com/study/login/")
driver.maximize_window()
try:
    ID = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.NAME, "login"))
    )
    ID.send_keys("[email protected]")
finally:
    driver.quit()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it worked. But how can you tell the dynamic ID from the static ID?
@KThong when you refresh the page the ID would change (dynamic) versus the name (which we used) would be static (doesn't change on refresh)
0

you dont need to tell the driver to wait till the element is present your can simply its xpath as it will executed only after page loads so use:

   ID = driver.find_element(By.XPATH, "/html/body/div[2]/div/div[4]/div/div[2]/div/div/div/div/form/div[1]/div/dl[1]/dd/input")
   ID.send_keys("[email protected]")
   

using xpath to locating the element is very easy and easy to locate. just 1.inspect the element 2.right click on the element 3. copy full xpath

Comments

0

To send a character sequence to the Your name or email address field you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://www.englishforum.com/study/login/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href$='dismiss-notice'] > span.button-text"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='login']"))).send_keys("[email protected]")
    
  • Using XPATH:

    driver.get('https://www.englishforum.com/study/login/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(@href, 'dismiss-notice')]/span[@class='button-text']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='login']"))).send_keys("[email protected]")
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

englishforum

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.