3

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?

PNarimani
  • 614
  • 7
  • 23

1 Answers1

3

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")))
    
  • Using CSS_SELECTOR:

    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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • i understand it is not a solution to try to get elements depending on their fetched order, but i am trying to scrape whatsapp for example and want to fetch the last message i received in a chat. all message elements are identical in html. the only way to know if the element is the last is from its order. it can also be known by its precedent element that has "new message" text. Is there a way to force selenium to get elements in the right order? perhaps make it wait for the page to load or something? – Dominique Abou Samah May 09 '23 at 17:17