1

I have a python script logging into our company website to then get each row of data. It logs me in, locates the iframe where the table with row values is located, from there I try to locate a row in the iframe table and attached is a picture:

xpathexpression

Highlighting the correct xpath expression but I need all 31 values.

All my code works properly except this:

elem5 = browser.find_element_by_xpath("//td/label[contains(@id, 'driver')][1]")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

2

find_element_by_* returns a single webelement, where as you are looking for all the 31 elements. So you need find_elements* and you can use either of the following Locator Strategies:

  • Using css_selector:

    elements = driver.find_elements(By.CSS_SELECTOR, "td > label[id^='driver']")
    
  • Using xpath:

    elements = driver.find_elements(By.XPATH, "//td/label[starts-with(@id, 'driver')]")
    

To find all the desired elements ideally you need to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "td > label[id^='driver']")))
    
  • Using XPATH:

    elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//td/label[starts-with(@id, 'driver')]")))
    
  • 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