Pyderman
Pyderman

Reputation: 16237

How to capture all matches for an incrementing Xpath query in another single XPath query?

Twenty elements of the same type on a certain page can be found with following XPaths:

//ol/div[2]/div/div/div[2]/div[1]/div/div[3]/div/a[1]/div/div/div[2]/div/span //ol/div[2]/div/div/div[2]/div[2]/div/div[3]/div/a[1]/div/div/div[2]/div/span

up to

//ol/div[2]/div/div/div[2]/div[20]/div/div[3]/div/a[1]/div/div/div[2]/div/span

(The full XPath is actually /html/body/div/div[5]/div[4]/div[5]/div[1]/div[3]/div/div[2]/div[2]/div/div/ol/div[2]/div/div/div[2]/div[6]/div/div[3]/div/a[1]/div/div/div[2]/span but the above abbrevation works well).

What I want to achieve: Instead of looping over a range from 1 to 20 (as i), I wish to somehow modify the general Xpath:

//ol/div[2]/div/div/div[2]/div[i]/div/div[3]/div/a[1]/div/div/div[2]/div/span

So that I can capture all 20 matches in one go. How best can this be achieved?

Upvotes: 1

Views: 36

Answers (1)

splash58
splash58

Reputation: 26153

You can use position function in condition [position() >= 1 and position() <= 20]

//ol/div[2]/div/div/div[2]/div[position() >= 1 and position() <= 20]/div/div[3]/div/a[1]/div/div/div[2]/div/span

Upvotes: 3

Related Questions