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?
-
1Hello, Swetha Singh, can you please share your html codeQAMember– QAMember2015年07月27日 10:17:07 +00:00Commented Jul 27, 2015 at 10:17
1 Answer 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.
Explore related questions
See similar questions with these tags.