How to select last element in XPath?

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.

Question tagged: XPath

Related Posts

Web Scraping With Ruby

Introduction to web scraping with Ruby. How to handle http connections, parse html files for data, best practices, tips and an example project.

Parsing HTML with Xpath

Introduction to xpath in the context of web-scraping. How to extract data from HTML documents using xpath, best practices and available tools.

Web Scraping With PHP 101

Introduction to web scraping with PHP. How to handle http connections, parse html files for data, best practices, tips and an example project.