问题
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.
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>
回答1:
You are trying to locate a link by using the following CssSelectors:
a[text='This is a link']
a[innertext='This is a link']
As per Issue#987 and Issue#1547:
The :contains pseudo-class isn't in the CSS Spec and is not supported by either Firefox or Chrome (even outside WebDriver).
You can find a detailed discussion in selenium.common.exceptions.InvalidSelectorException with “span:contains('string')”
Solution
As per the HTML you can use either of the following solutions:
CssSelector using the attribute
title
:"a[title='seleniumframework']"
CssSelector using the attribute
href
:"a[href='http://www.seleniumframework.com']"
CssSelector using the attributes
title
andhref
:"a[title='seleniumframework'][href='http://www.seleniumframework.com']"
回答2:
The right CSS Selector for your case is:
a[title="seleniumframework"]
or
a[href="http://www.seleniumframework.com"]
You can also use this one: a[target="_blank"]
, but the ones above are more unique.
Hope it helps you!
回答3:
Since you are trying to use attribute selector, you can only select from available attributes that the hyperlink element currently has.
text is not available attribute in html so selecting it this way through css will not work.Rather you should try to use other attribute like a[title=seleniumframework] for example.
来源:https://stackoverflow.com/questions/53351028/finding-link-using-text-in-css-selector-is-not-working