简体   繁体   中英

Unable to retrieve all tr elements of a web table using selenium python

在此处输入图片说明

As seen in the picture, I have more than 1 table rows. However, I am unable to retrieve any of those table rows except for the first row.

Code:

roster_tbody = browser.find_element(By.XPATH, "//*[@id='tableDay']/tbody")
tr = roster_tbody.find_elements(By.TAG_NAME, "tr")
print("number of table rows: " + str(len(tr)))
for trEl in tr:
    print(trEl.text)

Expected output:
I should be seeing all dates, days, year and 'Full'(if applicable) being printed for each row.

Actual output:

在此处输入图片说明

What I've tried:

1)

tr = WebDriverWait(browser, 20).until(
    EC.visibility_of_all_elements_located((By.TAG_NAME, "tr"))
)

It does not work as well and return the first table row only.

2)

row_2 = browser.find_element(By.XPATH, "//*[@id='tableDay']/tbody/tr[2]")
print(row_2.text)

Outcome:

在此处输入图片说明

3) changing //*[@id='tableDay']/tbody to .//*[@id='tableDay']/tbody does not work as well

  1. Searching for tr 2 from elements panel: tr 2 was found and highlighted.

在此处输入图片说明

  1. Searching for tr from console panel: all 37 tr was found.

在此处输入图片说明

Any help is deeply appreciated!

尝试使用browser.find_elements_by_xpath("//*[@id='tableDay']/tbody/tr[2]")

Since there are lot of tr tags, I would assume scrolling to each element would be neccessary.

Also, I am using indexing to look for each web element.

Code :

roster_tbody = browser.find_elements(By.XPATH, "//*[@id='tableDay']/tbody/tr")
j = 1
for i in range(len(roster_tbody)):
    element = browser.find_element(By.XPATH, f"(//*[@id='tableDay']/tbody/tr)[{j}]")
    browser.execute_script("arguments[0].scrollIntoView(true);", element)
    print(element.get_attribute('innerHTML'))
    j = j + 1 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM