1

I am working on a scraping data from a series of tables on an HTML website. The website has varying numbers of tables depending on the input, so I am trying to use: .find_elements_by_xpath("//table") to simply get a list with all the table elements on the page.

However, it is only returning the first table on the page in this case. When I use find_element_by_xpath(//table[2]), it returns the other table, but using find_elements does not.

The website portal is here, just press send at the bottom. (I am trying to get the data from the results page). I am using Selenium in Python on the Firefox Webbrowser.

Interesting note, the header table has an XPath of table[1], which begs the question of where is table[0].

5
  • how do you know selenium does not return all elements? Please show code, as it should return 2 tables. About xpath [1] - counter in xpath starts from 1, not 0. So [1] is really first table. and //table[0] does not exist =) Commented Aug 26, 2020 at 18:57
  • 2
    hey, opened this site again, and there is only 1 table now. Commented Aug 26, 2020 at 19:00
  • Hey there, so when I run the following code, it only returns a value of 1, as opposed to the 2 expected. (One table for the header of the page and the other for the data table). count = driver.find_elements_by_xpath("//table") print(len(count)) Commented Aug 26, 2020 at 22:29
  • did you try putting explicit wait before taking count? Commented Aug 27, 2020 at 0:08
  • @AmrutaPande Hi, Yes I am using an explicit wait before I take count. Again, if I use EC. presence_of_element_located(By. XPATH, "//table[2]) It loads table 2 just fine, but still, the other tables are not found by EC.presence_of_all_elements_located Commented Aug 28, 2020 at 1:50

1 Answer 1

2

The Key here is to wait properly before you navigate to new tab. As after clicking on send a new tab is getting opened. See below code:

driver.get('http://imed.med.ucm.es/Tools/rankpep.html')
WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, "//img[@src='/icons/ig_logo.png']")))

send = driver.find_element_by_xpath("//input[@value='Send']")
driver.execute_script("arguments[0].scrollIntoView();", send)
driver.execute_script("arguments[0].click();", send)

WebDriverWait(driver, 20).until(EC.number_of_windows_to_be(2)) #Wait for 2nd tab to open
driver.switch_to.window(driver.window_handles[1])
count = driver.find_elements_by_xpath("//table")
print(len(count))

Out Put: As on new page there are two tables, it has printed 2 enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Hey thanks for this. Strangely, this code didn't work with a Firefox webdriver, but it inspired me to try Chromedriver. Somewhat inexplicably all the code works now. Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.