🚀 We are hiring! See open positions

How to select last element in XPath?

by Bernardas Alisauskas Dec 26, 2023 1 min read

To select the last element using XPath the last() function can be used in a predicate. The last() function returns true if the element is the last sibling in the current tree level. See this interactive example:

<html> <div> <p>ignore</p> <p>ignore</p> <p>select</p> </div> </html>

Unlike other programming languages, XPath doesn't support reverse indexing (i.e. [-1]) so the last() function should be used to select the last element.

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.