[英]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.如图所示,我有超过 1 个表格行。 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) 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) 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 3) 将//*[@id='tableDay']/tbody
更改为.//*[@id='tableDay']/tbody
也不起作用
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.由于有很多 tr 标签,我认为滚动到每个元素是必要的。
Also, I am using indexing to look for each web element.此外,我正在使用索引来查找每个 Web 元素。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.