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?

3 Answers 3

3

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

You can find a couple of detailed discussions in:

Sign up to request clarification or add additional context in comments.

4 Comments

what can you say about the second answer here? Is it correct? he says it is possible to locate web element based on it's text By.cssSelector with Selenium.
The author didn't provide any links to the docs/specs and I'm not sure if it is a speculation. Need some time to see the specs.
Please let me know if you checked that and have any new insights here
Sure. I'll do that.
-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']"));

2 Comments

Hi, According to the example in my question, I tried a[textContent='Daniel'] also with innertext, it wouldnt recognize my element.
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 ?
-1

For those who are looking for a solution nowday, there is a proper solution. Within css we can now search a value like in following example:

By.cssSelector("button[id*='btn_enter'])

Button identification in my case looks like: <button id="btn_enter_" type="submit" ...>

1 Comment

This works for element attributes, the question is about element text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.