Home > Enterprise >  Selenium python cannot find element
Selenium python cannot find element

Time:02-05

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()

CodePudding user response:

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()

CodePudding user response:

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

  •  Tags:  
  • Related