trying to get all p tag text between two h2 tags
xpath
Solution
So you just want to get all the `p` tags where they are between two specific `h2` tags. The xpath query is exactly as it sounds.
//p[
preceding-sibling::h2[span='Title1'] and
following-sibling::h2[.='Second Title I want to stop collecting p tags after']
]
The query could be simplified by selecting all `p` where the first preceding `h2` element is the starting element. In other words, there are no other `h2` previous siblings between the current `p` and the header.
//p[preceding-sibling::h2[1][span='Title1']]
Problem
``` <h2><span>Title1</span></h2> <p>text I want</p> <p>text I want</p> <p>text I want</p> <p>text I want</p> <h2>Second Title I want to stop collecting p tags after</h2> ``` I can get p tags by identifying the text within the h2, then get preceeding-sibling::p but this grabs all p tags to the end of the DOM. I have tried to use the "and" selector to essentially declare a start and end but it returns null. I must be missing something here but I've been stuck on this for quite a while. I cannot predict how many p tags I need so an index number on the p element is not going to help me in this case. Here is the xpath I used to get all of the following p tags after an h2. the problem is it grabs all p tags to the end of the DOM. ``` //span[contains(text(), "Title1")]/ancestor::h2/following-sibling::p ```