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?

  • 1
    Hello, Swetha Singh, can you please share your html code – QAMember Jul 27 '15 at 10:17
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.