0

I have HTML that (somehow) includes two elements with the same ID. Only one of them is clickable, and to identify the one I want to click, I am using:

driver.find_elements_by_id('someID')[1]

Now I want to convert this to an explicit wait. If there was only one element with that ID (or any selector), I would do this:

WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.ID, 'someID')))

How do I convert that statement to identify the second item with that ID, not the first? The alternative is to use the full xpath of the element, but I thought finding the second element with the ID would be more stable.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
OverflowingTheGlass
  • 2,324
  • 1
  • 27
  • 75

1 Answers1

0

Instead of element_to_be_clickable() you can induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using ID:

    WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.ID, "someID")))[1].click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "#someID")))[1].click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@id='someID']")))[1].click()
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • this gave me a standard `TimeoutException1`, as if the elements are not found. however `driver.find_elements_by_id('getDimension')` works perfectly. – OverflowingTheGlass Jul 23 '20 at 21:20