🚀 We are hiring! See open positions

How to select elements of a specific position in XPath?

by Bernardas Alisauskas May 29, 2023 1 min read

To select elements of a specific position the position() function can be used with a predicate:

<!-- select all product detail urls --> <html> <div> <h2>Product 1</h2> <a href="/product/1/reviews">reviews</a> <a href="/product/1/details">details</a> <a href="/product/1/refunds">refunds</a> </div> <div> <h2>Product 2</h2> <a href="/product/2/reviews">reviews</a> <a href="/product/2/details">details</a> <a href="/product/2/refunds">refunds</a> </div> </html>

Note that using position() when web scraping is not recommended as HTML element position is likely to change. Instead other methods like selection by attribute value is a more reliable way if applicable.

Scale Your Web Scraping
Anti-bot bypass, browser rendering, and rotating proxies — all in one API. Start with 1,000 free credits.
No credit card required 1,000 free API credits Anti-bot bypass included
Not ready? Get our newsletter instead.