percentum percentum - 1 year ago 78
HTML Question

XPATH: p element who's immediate preceding sibling is a p

I am trying to select the first p element who's direct preceding sibling is also a p element. I have highlighted the below code with example of what i am looking for please.

<div class="vacancy-left">
<h2>Machine Operator / Print Operator</h2>
<h3>Vacancy Ref:</h3>
<p>Are you self-motivated with a..</p> <!-- THIS NODE -->
<p>The ideal candid...</p>
<p>Hours of Work Monday- Friday 07.30-16.30</p>
<p>Start pay from £8.00 per hour</p>
<p> Immediate Start</p>
<h2><a href="../../contact.php">Apply For This Vacancy</a></h2>
<p><a href="../industrial.htm">Return to Industrial Vacancies</a></p>

I've tried a couple things such as .//p[preceding-sibling::p] but that selects a few p elements that have any preceding element as a p.

Answer Source

You can use //p[preceding-sibling::*[1][self::p]] to select all p elements where the nearest preceding sibling element is also a p element, however it would select more than one p in your sample as several p elements have another p element as the nearest preceding sibling element.