1

Want to click on third li from second ul:

   <ul id="select-123" style="width: 1180px; display: none;">
   <li class="" style="display:none;">
   <li class="">
   <li class="">
   <li class="">
   </ul>

   <ul id="select-123" style="width: 1180px; display: none;">
   <li class="" style="display:none;">
   <li class="">
   <li class="">
   <li class="">
   </ul>

Code I am trying is to select third li from second ul which does not work:

    driver.findElements(By.css(ul[id*='select-123'])).then(function(elems) {
    elems[2].then(function(lis) {
    driver.findElement(By.css("ul[id*='select-123'] li:nth-child(3)")).click();
    });
    });
1
  • 1
    you have two elements with the same id, id's should be unique Commented Aug 2, 2016 at 13:08

3 Answers 3

1

Try to use XPath:

driver.findElement(By.xpath('//ul[2]/li[3]'))
Sign up to request clarification or add additional context in comments.

Comments

1

In addition to the answer by @Andersson you also have made the mistake of having two IDs. In HTML, IDs should be unique. Simply having a unique ID would have avoided this in the first place, but if you do not have control over the HTML, and someone else wrote bad HTML, the XPath solution of Andersson would be good.

Comments

1

Actually you are finding elements as a list then you are using second ul element from the list, but when you are going to find child element of second ul element, you are providing driver.findElement which will find from whole page, so instead of this you need to find element on second ul element context as below :-

driver.findElements(By.css(ul[id*='select-123'])).then(function(elems) {
    elems[2].then(function(lis) {
      lis.findElement(By.css("ul[id*='select-123'] li:nth-child(3)")).click();
    });
});

Hope it works..:)

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.