Example document:
<h4 class="bla">July 12</h4>
<p>Tim</p>
<p>Jon</p>
<h4 class="bla">July 13</h4>
<p>James</p>
<p>Eric</p>
<p>Jerry</p>
<p>Susie</p>
<h4 class="date">July 14</h4>
<p>Kami</p>
<p>Darryl</p>
What I want to do is grab all p nodes that were posted on July 13. Note that they are siblings of h4 and not children. So in this example, I'd like to get the p nodes that hold the following names: James, Eric, Jerry, and Susie.
I got close with the following, but it chose all p nodes that came after the July 13th h4 node, since they're all siblings. In other words, it didn't have a stop condition.
//h4[string() = 'July 13']/following-sibling::p
We can use the XPath following sibling axis to find this. So, for this scenario, the XPath expression will be. And we need to identify its sibling “div ” element, as shown below. However, if numerous siblings have the same node, XPath will recognise all of the different elements.
We can use the concept of following-sibling in xpath for identifying elements in Selenium. It identifies the siblings of the context node. The siblings should be located at the equal level of the existing node and should have the same parent.
The preceding-sibling axis indicates all the nodes that have the same parent as the context node and appear before the context node in the source document.
Use this XPath:
//p[preceding-sibling::h4[1][. = 'July 13']]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With