如何有效地迭代表数在硒Webdriver使用Python?



请建议我如何有效地更改tr[1],[2],[3]或N个数字,按照网站提供的表格自动更改

代码:

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()

尝试如下:

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.

您可以考虑使用find_elements_by_xpath('.//tr')而不是find_element_by_xpath

或者,您可以使用带有try-except的简单for循环。这不是最优雅的,但它会让你得到所有的N行之前,由于异常。

最新更新