【问题标题】:Python / Selenium - using a conditional in an xpathPython / Selenium - 在 xpath 中使用条件
【发布时间】:2016-05-03 03:21:50
【问题描述】:

我需要在一些结构基本相同的网页中找到特定元素,使用以下xpath:

//*[@id="detailPCTtableHeader"]/tbody/tr[10]/td[2]/div/span/span[1]/text()

问题是在某些页面中tr[10] 是tr[11]。

有没有办法告诉 Selenium 搜索tr[10] 或 tr[11]?

【问题讨论】:

    标签: python selenium xpath


    【解决方案1】:

    您可以使用position() 来评估索引:

    element = driver.find_element_by_xpath("id('detailPCTtableHeader')/tbody/tr[position()=10 or position()=11]/td[2]/div/span/span[1]")
    

    【讨论】:

      【解决方案2】:

      “有没有办法告诉 Selenium 搜索 tr[10] 或 tr[11]?”

      使用position():

      tr[position() = 10 or position() = 11]
      

      或者,如果tr[10] 和tr[11] 一次只存在一个,这意味着它始终是最后一个tr,您可以简单地使用last():

      tr[last()]
      

      【讨论】:

        猜你喜欢
        • 2014-09-15
        • 1970-01-01
        • 1970-01-01
        • 2015-12-28
        • 2020-03-07
        • 1970-01-01
        • 2020-09-04
        • 1970-01-01
        • 2012-12-21
        相关资源
        最近更新 更多