Order of found elements in Selenium

I’m using selenium with python to interact with a webpage.
There is a table in the webpage. I’m trying to access to its rows with this code:

rows = driver.find_elements_by_class_name("data-row")

It works as expected. It returns all elements of the table.
The question is, is the order of the returned elements guaranteed to be the same as they appear on the page?
For example, Will the first row that I see in the table in browser ALWAYS be the 0th index in the array?


You shouldn’t be depending on the fact whether Selenium returns the elements in the same order as they appear on the webpage or DOM Tree.

Each WebElement within the HTML DOM can be identified uniquely using either of the Locator Strategies.

Though you were able to pull out all the desired elements using find_elements_by_class_name() as follows:

rows = driver.find_elements_by_class_name("data-row")

Ideally, you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using CLASS_NAME:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "data-row")))

    element = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".data-row")))
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@class='data-row']")))
  • 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

You can find a detailed discussion in WebDriverWait not working as expected