1

In Selenium, are there any options for CSS selectors with "contains" similar to Xpath where we check with contains[text(),....].

In the following example, I have given the CSS and Xpath

<div class="abcjha">
  <span>
    <a class="test" ng-tracking="test1" data-tracking="abc">Daniel</a>
  </span>
</div>

CSS : a[data-tracking='abc']
Xpath : //div/span/a[contains(text(),'Daniel')]

Similar to Xpath above, is there an option in CSS to check for the inner text ? When I browsed about it I saw this option

==> css=button:contains("Log In")

but it is not working for me.

Is there any option?

user16120973
  • 27
  • 2
  • 8

2 Answers2

2

No, css_selector for Selenium still doesn't supports :contains("text") method.

You can find a couple of detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

The contains() pseudo-class may not work with browsers that don't natively support CSS selectors. Also, it has been deprecated from CSS3 specification.

so the alternative is using innerText

WebElement cell = driver.findElement(By.cssSelector("td[innerText='Item 1']"));

or textContent

WebElement cell = driver.findElement(By.cssSelector("td[textContent='Item 1']"));
taqin
  • 29
  • 4
  • Hi, According to the example in my question, I tried a[textContent='Daniel'] also with innertext, it wouldnt recognize my element. – user16120973 Mar 10 '22 at 15:53
  • I tried to reach the parent and then the child, This helped to reach the child div.td>span>a but when I add innertext/textContent like div.td>span>a[innerText="Daniel AG"] it wouldnt identify the element. Any help on this ? – user16120973 Mar 10 '22 at 16:10