Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to get all p tag text between two h2 tags

Tags:

xpath

<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
like image 815
Chris Hawkes Avatar asked Dec 04 '25 03:12

Chris Hawkes


1 Answers

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']]
like image 139
Jeff Mercado Avatar answered Dec 06 '25 18:12

Jeff Mercado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!