Home > Back-end >  Why is selenium picking up more elements than HTML inspect element shows?
Why is selenium picking up more elements than HTML inspect element shows?

Time:08-22

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.

CodePudding user response:

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.

CodePudding user response:

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.

  • Related