AutomationTest
AutomationTest

Reputation: 109

Unable to find CSS selector by :contains()

Unable to find CSS selector using :contains().

enter image description here

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

Answers (2)

undetected Selenium
undetected Selenium

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:


Alternative

To locate the element with text as Summary you can use either of the following :

  • Using first-child:

    ul#id li:first-child > a
    
  • Using nth-child():

    ul#id > li:nth-child(1) > a
    

tl; dr

References:

Upvotes: 1

PDHide
PDHide

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

Related Questions