【问题标题】:Unable to locate the element by CSS selector and Xpath无法通过 CSS 选择器和 Xpath 定位元素
【发布时间】:2019-12-25 12:19:20
【问题描述】:

我正在尝试使用 CSS 选择器内部文本定位此标记

<a href="/introduction-to-selenium.html">1) Introduction</a>

我用过xpath:

a.contains("1) Introduction")

但它不起作用。谁能帮帮我?

【问题讨论】:

  • a.contains("") 不是 xpathcss selector。无论如何都不能使用css selector文字定位。

标签: selenium selenium-webdriver xpath css-selectors xpath-1.0


【解决方案1】:

您使用的选择器不是 xpath 之一,正确的 xpath 选择器应该是这样的:

//a[contains(string(), "1) Introduction")]

【讨论】:

    【解决方案2】:

    要定位元素,您可以使用以下任一Locator Strategies

    • 链接文本

      1) Introduction
      
    • 部分链接文本

      Introduction
      
    • cssSelector

      a[href^='/introduction-to-selenium']
      
    • xpath

      //a[starts-with(@href, '/introduction-to-selenium') and contains(., 'Introduction')]
      

    结尾

    selenium.common.exceptions.InvalidSelectorException with “span:contains('string')”

    【讨论】:

      【解决方案3】:

      您也可以在 xpath 中使用 contains 来识别标签。

      eg : //a[contains(text(),'Introduction')]

      页面中不应有多个“介绍”。

      【讨论】:

        猜你喜欢
        • 2021-11-15
        • 1970-01-01
        • 1970-01-01
        • 2019-07-20
        • 2021-08-21
        • 2020-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多