【问题标题】: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.
    

    【讨论】:

    • 非常感谢,不可思议
    【解决方案2】:

    您可以考虑使用find_elements_by_xpath('.//tr') 代替find_element_by_xpath

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

    【讨论】:

      猜你喜欢
      • 2014-07-23
      • 2013-01-12
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多