I am using Ghost Inspector and I have an element on the page that has an attribute that we use for QA. But there are more than one of them and I need to find the second one on the page. If i just select the attribute it grabs the first one on the page.


data-qa="settings:unfollow-business" this is my attribute that I can select.

  • Also this was my first question EVER Feb 12, 2019 at 23:03
  • Please share the error/exception along with the intended action. Also please share the html as well Feb 13, 2019 at 1:51

3 Answers 3


If you are 100% sure that the second element is the one you want, you can try the below.

var secondwebelement = driver.findElements(By.cssSelector(data- 

No need to go over a list since you should only query those things you actually need because querying over the browser is slow and expensive (especially when your tests run in a selenium grid).

Just use the Selector "*[data-qa="settings:unfollow-business"]:nth-of-type(2)" and you'll actually get only the second element.

But just as advice, using the position in a list makes for rather unstable selectors, so you might want to look into using the structure of your application rather than the position.


Basically you need to generate List of Webelements (and put there all items with given selectors). The code higher might not work if you are using Java8 (there is no var there).

In that case you need to use:

// create list of elements
List<WebElement> elements = driver.findElements(By.cssSelector(data-qa="settings:unfollow-business"))[1];

// now access element from list above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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