[英]How to efficiently iterate table number in Selenium Webdriver using Python?
Please suggest me how to changes efficiently tr[1], [2], [3] or N numbers, following the table provided on the website automatically请建议我如何有效地更改 tr[1]、[2]、[3] 或 N 个数字,按照网站上提供的表格自动进行
code :代码 :
browser.find_element_by_xpath('//*[@id="event"]/tbody/tr[1]/td[9]').click()
browser.find_element_by_xpath('//*[@id="event"]/tbody/tr[2]/td[9]').click()
browser.find_element_by_xpath('//*[@id="event"]/tbody/tr[3]/td[9]').click()
You can consider using find_elements_by_xpath('.//tr')
instead of find_element_by_xpath
.您可以考虑使用
find_elements_by_xpath('.//tr')
而不是find_element_by_xpath
。
Alternatively, you can use a simple for loop with a try-except.或者,您可以使用带有 try-except 的简单 for 循环。 It's not the most elegant, but it will get you all N rows before going out due to the exception.
它不是最优雅的,但由于异常,它会在出门前为您提供所有 N 行。
Try like below:尝试如下:
options = browser.find_elements_by_xpath('//*[@id="event"]/tbody/tr') # this xpath should highlight all the tr tags in the DOM
for opt in options:
opt.find_element_by_xpath('./td[9]') # this should get the 9th td tag of that particular tr. Use a "." in the xpath to find element within an element.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.