Reputation: 247
I am automating a website using Selenium C#. I am getting NoSuchElementException while trying to click element using CSS selector. But when I use xpath things are working. Can anyone help me understand why I am having this issue while using CSS selector.
Xpath value used.
//*[@id="primary-navigation"]/ul/li[2]/a
CSS Selector value used.
#primary-navigation > ul > li:nth-child(2) > a
Upvotes: 1
Views: 516
Reputation: 6939
xpaths li[2]
gives you the second li element
css li:nth-child(2)
gives you the li element that is the second child of its parent
for example
<ul>
<p> something </p>
<li> this will be returned </li>
</ul>
here xpath li[2]
will return nothing (or an error/exception), cause there is only one li element
BUT css li:nth-child(2)
will return the li element since it is the 2nd child of parent ul
So most probably the selectors are working properly, you just can't "translate" between xpath and css selectors "directly"
Upvotes: 2