1

When i try to locate one element with selellium it fails

driver = webdriver.Chrome(executable_path = r'./chromedriver.exe')

driver.get("http://eltiempo.com/login")
try:
    element = WebDriverWait(driver, 30).until(
        EC.presence_of_element_located((By.XPATH, "//*[@id='username']"))
    )

finally:
    driver.quit()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Nicolas Rondon
  • 228
  • 2
  • 10
  • Any reason for using XPATH over id selector?. Please try with id selector, id is unique in entire DOM and id selector works much better than xpath. Even [xpath works differently in different browsers](https://stackoverflow.com/questions/23053632/is-xpath-is-different-for-different-browser), so if you want to run your code in all machine id selector is much better – Sajid Dec 14 '20 at 05:15

1 Answers1

1

To send a character sequence to the username field as the element is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      driver.get("https://www.eltiempo.com/login")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.iframe-login")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']"))).send_keys("Nick Rondon@stackoverflow.com")
      
    • Using XPATH:

      driver.get('https://www.eltiempo.com/login')
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='iframe-login']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='username']"))).send_keys("Nick Rondon@stackoverflow.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:

eltiempo


Reference

You can find a couple of relevant discussions in:

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