Max Jacobi
Max Jacobi

Reputation: 183

Select nth element inside nth element with XPath

How to select second input in second row?

<div class="row">
  <whatever>
    <input/>
  </whatever>
  <whatever>
    <input/>
  </whatever>
</div>
<div class="row">
  <whatever>
    <input/>
  </whatever>
  <whatever>
    <input/> <!-- select this -->
  </whatever>
</div>

I tried (//div[@class='row'])[2](//input)[2] but it doesn't work.

I don't want (//div[@class='row'])[2]/whatever[2]/input because "whatever" is unknown amount of nested nodes i.e. it might be something like this:

<whatever1>
  <whatever2>
    <whatever3>
      <input/>
    </whatever3>
  </whatever2>
</whatever1>

Upvotes: 1

Views: 424

Answers (2)

Eldar
Eldar

Reputation: 10790

Actually you were almost there here the fixed version of your first try :

((//div[@class='row'])[2]//input)[2]

(//div[@class='row'])[2]//input Matches all the inputs. All you need to select the second match.

Upvotes: 2

cruisepandey
cruisepandey

Reputation: 29362

You can use the below xpath :

//div[@class='row'][2]/descendant::input[2]

or

((//div[@class='row'])[2]/descendant::input)[2]

Explanation :

input is descendant not direct child so you can not do

(//div[@class='row'])[2](//input)[2]

Upvotes: 4

Related Questions