EdXX
EdXX

Reputation: 890

Protractor - select the second or third element at the same level

I know that there are some questions-answers similar to the topic but mine is a bit different.

I also know that we have functions like first() and last() in Protractor but I'm wondering whether it's possible to to something what is possible in regular Webdriver like:

//div[@attr='header']/div/div[2]

Where you can indicate the second sibling by [2] ? I've tried that and didn't work but maybe here it looks a bit differently?

Upvotes: 0

Views: 397

Answers (1)

tehbeardedone
tehbeardedone

Reputation: 2858

There are a couple of different ways to accomplish this. Along the same lines of using .last() and .first(), you can also use .get(<index>). Indexes start at 0. So element.all(by.xpath('//div[@attr='header']/div/div').get(2) would work for this scenario.

You can also use :nth-child() css selector to find siblings. Indexes for nth-child start at 1.

<div>
  <span>1</span>
  <span>2</span>
  <span>3</span>
</div>

In the above html snippet, if I wanted to select the 3rd span I would use 'div > span:nth-child(3)'

Upvotes: 1

Related Questions