Why is selenium picking up more elements than HTML inspect element shows?

Question:

So, I have a simple python script which is supposed to count the number of rows in a table using selenium.

The thing is, when I print using len() it shows 9 but when I inspect the element there are only 4 tr.

How is this happening?

Here is the snippet of the len:

    try:
        WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//table/tbody/tr')))
        td = driver.find_elements(By.XPATH, '//table/tbody/tr')
        print(len(td))
    except Exception as e:
        print (e)

And here is the sample html structure of the website I am trying to scrape.

Side info: the number of rows below is being defined by an AJAX.

<tbody>
 <tr>...</tr>
 <tr>...</tr>
 <tr>...</tr>
 <tr>...</tr>
</tbody>

I am also pretty sure that, the above snippet is the one pointing out by xpath as it was the one being highlited by find function of dev console of chrome.

Answers:

You should wait for all the table loaded before counting the found td.
visibility_of_element_located((By.XPATH, '//table/tbody/tr') is waiting for the first row in the table only.

Answered By: Prophet

I was able to figure it out.

Apparently, the calendar being displayed also uses Tables and since I started my xpath with // it counts the TR on both tables.

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.