【问题标题】:How to efficiently iterate table number in Selenium Webdriver using Python?如何使用 Python 在 Selenium Webdriver 中有效地迭代表号?
【发布时间】:2021-10-25 12:03:36
【问题描述】:
请建议我如何根据网站上自动提供的表格有效地更改 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()
【问题讨论】:
标签:
python
python-3.x
selenium
selenium-webdriver
【解决方案1】:
尝试如下:
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.