无法通过CSS选择器和Xpath定位元素

时间:2019-08-20 09:50:06

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

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

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

我使用了xpath:

a.contains("1) Introduction")

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

3 个答案:

答案 0 :(得分:0)

您使用的选择器不是xpath之一,正确的xpath选择器将如下所示:

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

答案 1 :(得分:0)

要找到元素,可以使用以下Locator Strategies中的任何一个:

  • linkText

    1) Introduction
    
  • partialLinkText

    Introduction
    
  • cssSelector

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

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

Outro

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

答案 2 :(得分:0)

您还可以在xpath中使用包含来识别标签。

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

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