0

I was trying to locate table column containing text 'Name', and it fails while using cssSelector with 'Unable to locate element' but works with XPath.

I tried all the 3 below options with different browsers but it throws error:

driver.findElement(By.cssSelector("td:contains('Name')"));
driver.findElement(By.cssSelector("td[textContent = 'Name']"));
driver.findElement(By.cssSelector("td[innerText = 'Name']"));

Whereas same can be located by XPath:

driver.findElement(By.xpath("//td[contains(text(),'Name')]"));

Can anyone explain please why it is failing for the css and is there anything which I am missing out?

alecxe
11.4k11 gold badges52 silver badges107 bronze badges
asked Jul 27, 2015 at 10:14
1
  • 1
    Hello, Swetha Singh, can you please share your html code Commented Jul 27, 2015 at 10:17

1 Answer 1

1

cssSelector does not have selector that works with text. That's why it does not work :)

You need to use an alternate way. Why not xpath if it works fine? Or hierarchical CSS approach. That should work fine as well.

demouser123
3,5325 gold badges30 silver badges41 bronze badges
answered Jul 28, 2015 at 9:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.