1

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

1
  • li:nth-child(n) != li[n] Commented Oct 14, 2015 at 14:02

1 Answer 1

2

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"

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

Comments

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.