Specify only one leaf node with xpath from a given result

Viewed 21

given

<a>
  <div><span></span></div>
  <div></div>
  <div><span></span></div>
  <div><span></span></div>
</a>

I want to only select n-th span

say, only 2nd span

You CANNOT do this and declare victory:

//a/div[1]/span Selects first span //a/div[2]/span selects second span tadaaa I win.

no

Imagine the structure of the HTML is dynamic.

I tried for example

//a//span[2] but this doesn't work

E.g. after I want to //a/div/span get all leaflet spans, then select the second one for example

1 Answers

Will this

(//span)[2]

or this

(//a//span)[2]

work for you?

Related