Reputation: 109
Unable to find CSS selector using :contains()
.
I have followed the instructions from https://www.browserstack.com/guide/css-selectors-in-selenium at #5 – Inner text but still, no result is shown, Can someone please help me/Tell me how to find Web element using text, CSS only
Here is Sample Dom
<ul id='id'>
<li class='class'>
<a class='class_class2' href="/Myaccount/summary">"Summary"</a>
<li class='class'>
<a class='class_class2' href="/Myaccount/Profile">"Profile"</a>
</ul>
Here : a:contains('Summary')
Upvotes: 1
Views: 19282
Reputation: 193388
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 couple of relevant detailed discussion in:
To locate the element with text as Summary you can use either of the following css-selectors:
Using first-child
:
ul#id li:first-child > a
Using nth-child()
:
ul#id > li:nth-child(1) > a
References:
Upvotes: 1
Reputation: 19989
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference
https://stackoverflow.com/a/4781167/6793637
Contains: is no longer available
you should use xpath
to find elements using innerText
xpath:
//a[contains(text(),"Summary")]
You can get exact match as
//a[text()="Summary"]
Upvotes: 3