     [Answers](https://scrapfly.io/blog)   /  [data-parsing](https://scrapfly.io/blog/tag/data-parsing)   /  [How to select last element in XPath?](https://scrapfly.io/blog/answers/how-to-select-last-element-in-xpath)   # How to select last element in XPath?

 by [Bernardas Alisauskas](https://scrapfly.io/blog/author/bernardas) Apr 19, 2023 1 min read [\#data-parsing](https://scrapfly.io/blog/tag/data-parsing) [\#xpath](https://scrapfly.io/blog/tag/xpath) 

 [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-select-last-element-in-xpath "Share on LinkedIn")    

 

 

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:

&lt;html&gt; &lt;div&gt; &lt;p&gt;ignore&lt;/p&gt; &lt;p&gt;ignore&lt;/p&gt; &lt;p&gt;select&lt;/p&gt; &lt;/div&gt; &lt;/html&gt; 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 

 [Start Free](https://scrapfly.io/register) [View Docs](https://scrapfly.io/docs/onboarding) 

 Not ready? Get our newsletter instead. 

 

## Explore this Article with AI

 [ ChatGPT ](https://chat.openai.com/?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-select-last-element-in-xpath) [ Gemini ](https://www.google.com/search?udm=50&aep=11&q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-select-last-element-in-xpath) [ Grok ](https://x.com/i/grok?text=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-select-last-element-in-xpath) [ Perplexity ](https://www.perplexity.ai/search/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-select-last-element-in-xpath) [ Claude ](https://claude.ai/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-select-last-element-in-xpath) 



 ## Related Articles

 [  

 python data-parsing 

### Parsing HTML with Xpath

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

 

 ](https://scrapfly.io/blog/posts/parsing-html-with-xpath) [  

 data-parsing xpath 

### Ultimate XPath Cheatsheet for HTML Parsing in Web Scraping

Ultimate companion for HTML parsing using XPath selectors. This cheatsheet contains all syntax explanations with interac...

 

 ](https://scrapfly.io/blog/posts/xpath-cheatsheet) [  

 http 

### What is HTTP 406 Error? (Not Acceptable)

HTTP status code 406 generally means wrong Accept- header family configuration. Here's how to prevent it.

 

 ](https://scrapfly.io/blog/posts/what-is-http-error-406-not-acceptable) 

  ## Related Questions

- [ Q How to select elements by class in XPath? ](https://scrapfly.io/blog/answers/how-to-select-elements-by-class-in-xpath)
- [ Q How to select elements by text in XPath? ](https://scrapfly.io/blog/answers/how-to-select-elements-by-text-in-xpath)
- [ Q How to select elements by class using CSS selectors? ](https://scrapfly.io/blog/answers/how-to-select-elements-by-class-css-selectors)
- [ Q How to select any element using wildcard in XPath? ](https://scrapfly.io/blog/answers/how-to-select-elements-of-any-name-using-wildcards-in-xpath)
 
  



   



 Extract structured data with AI, **1,000 free credits** [Start Free](https://scrapfly.io/register)