Home > front end >  Does CSS Selector have contains text similar to xpath?
Does CSS Selector have contains text similar to xpath?

Time:03-10

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 >
  <span>
    <a  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?

CodePudding user response:

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

You can find a couple of detailed discussions in:

CodePudding user response:

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']"));
  • Related