在CSS选择器中使用文本查找链接不起作用 [英] Finding link using text in CSS Selector is not working

查看:55
本文介绍了在CSS选择器中使用文本查找链接不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 a [text ='这是一个链接'] a [innertext ='这是一个链接'] 他们两个都不起作用.

I am trying to locate below link by using a[text='This is a link'] and a[innertext='This is a link'] but both of them are not working.

我知道这可以通过使用XPath或其他方式来实现,但是我很好奇为什么我使用的CSS Selector无法正常工作.请参阅此链接.

I know this can be achieved by using XPath or other ways, but I am curious why CSS Selector that I have used is not working. refer this link.

<a title="seleniumframework" href="http://www.seleniumframework.com" target="_blank">This is a link</a>

推荐答案

您正尝试使用以下 CssSelector s查找链接:

You are trying to locate a link by using the following CssSelectors:

  • a [text ='这是一个链接']
  • a [innertext ='这是一个链接']

根据 问题#987 Issue#1547 :

:包含伪类不在CSS规范中,并且Firefox或Chrome(甚至在WebDriver外部)也不支持.

The :contains pseudo-class isn't in the CSS Spec and is not supported by either Firefox or Chrome (even outside WebDriver).

您可以在 selenium.common.exceptions中找到详细的讨论.InvalidSelectorException,带有"span:contains('string')"

根据HTML,您可以使用以下任一解决方案:

As per the HTML you can use either of the following solutions:

  • CssSelector 使用属性 title :

"a[title='seleniumframework']"

使用属性 href

  • CssSelector :

    "a[href='http://www.seleniumframework.com']"
    

  • CssSelector 使用属性 title href :

    "a[title='seleniumframework'][href='http://www.seleniumframework.com']"
    

  • 这篇关于在CSS选择器中使用文本查找链接不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆