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("dkthong2010@gmail.com")
finally:
    driver.quit()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
KThong
  • 1

3 Answers3

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("dkthong2010@gmail.com")
finally:
    driver.quit()
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
Andrew Ryan
  • 1,489
  • 3
  • 15
  • 21
  • Thanks, it worked. But how can you tell the dynamic ID from the static ID? – KThong Feb 04 '22 at 16:06
  • @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) – Andrew Ryan Feb 04 '22 at 16:10
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("dkthong2010@gmail.com")
   

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

CodingBee
  • 99
  • 3
  • 7
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("dkthong2010@gmail.com")
    
  • 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("dkthong2010@gmail.com")
    
  • 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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352