1

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')

Mate Mrše
  • 7,328
  • 9
  • 29
  • 68

2 Answers2

1

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"] 
Cosmin
  • 2,244
  • 2
  • 22
  • 37
PDHide
  • 15,234
  • 2
  • 19
  • 35
0

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:

undetected Selenium
  • 151,581
  • 34
  • 225
  • 281