     [Answers](https://scrapfly.io/blog)   /  [data-parsing](https://scrapfly.io/blog/tag/data-parsing)   /  [How to select all elements between two elements in XPath?](https://scrapfly.io/blog/answers/how-to-select-all-elements-between-two-known-elements-in-xpath)   # How to select all elements between two elements in XPath?

 by [Bernardas Alisauskas](https://scrapfly.io/blog/author/bernardas) Apr 18, 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-all-elements-between-two-known-elements-in-xpath "Share on LinkedIn")    

 

 

To select an element that is between two known elements in XPath we have several options. Let's see these few interactive examples:

1. If we can identify an anchor element we can restrict our selection using `preceding-sibling` or `following-sibling`:

&lt;article&gt; &lt;p&gt;ignore&lt;/p&gt; &lt;p&gt;ignore&lt;/p&gt; &lt;h2&gt;anchor&lt;/h2&gt; &lt;p&gt;select&lt;/p&gt; &lt;p&gt;select&lt;/p&gt; &lt;p&gt;select&lt;/p&gt; &lt;h2&gt;title2&lt;/h2&gt; &lt;p&gt;ignore&lt;/p&gt; &lt;p&gt;ignore&lt;/p&gt; &lt;/article&gt; Above we select all `<p>` elements that have the first preceding `<h2>` element with text `anchor`.

2. If we know the count of unique preceding or following elements we can use `count()`:

&lt;article&gt; &lt;p&gt;ignore&lt;/p&gt; &lt;p&gt;ignore&lt;/p&gt; &lt;h2&gt;anchor&lt;/h2&gt; &lt;p&gt;select&lt;/p&gt; &lt;p&gt;select&lt;/p&gt; &lt;p&gt;select&lt;/p&gt; &lt;h2&gt;title2&lt;/h2&gt; &lt;p&gt;ignore&lt;/p&gt; &lt;p&gt;ignore&lt;/p&gt; &lt;/article&gt; Here, we select all `<p>` elements that have exactly one preceding `<h2>` element. Element counting is less reliable than using an anchor element but is often much easier to implement.

XPath provides a lot of flexibility in selecting elements as we can navigate the HTML tree in every direction and match elements by any attribute.
For more on XPath, see our [XPath introduction tutorial](https://scrapfly.io/blog/posts/parsing-html-with-xpath)



 

   Table of Contents















 

   Join the Newsletter  Get monthly web scraping insights 

 

  



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-all-elements-between-two-known-elements-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-all-elements-between-two-known-elements-in-xpath) [ Grok ](https://x.com/i/grok?text=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-select-all-elements-between-two-known-elements-in-xpath) [ Perplexity ](https://www.perplexity.ai/search/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-select-all-elements-between-two-known-elements-in-xpath) [ Claude ](https://claude.ai/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-select-all-elements-between-two-known-elements-in-xpath) 



 ## Related Articles

 [  

 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) [  

 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 css-selectors 

### Parsing HTML with CSS Selectors

Introduction to using CSS selectors to parse web-scraped content. Best practices, available tools and common challenges ...

 

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

  ## Related Questions

- [ Q How to select sibling elements in XPath? ](https://scrapfly.io/blog/answers/how-to-select-sibling-elements-using-xpath)
- [ Q Is it possible to select preceding siblings using CSS selectors? ](https://scrapfly.io/blog/answers/how-to-select-preceding-sibling-element-css-selectors)
- [ Q How to select following siblings using CSS selectors? ](https://scrapfly.io/blog/answers/how-to-select-following-sibling-element-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)