I have the following code:
<div id="mydiv">
<h1>Some title</h1>
<p>don't select me</p>
<p>select me 1</p>
<p>select me 2</p>
<p>select me 3</p>
<p>don't select me</p>
</div>
I need to select p[2] through p[4].
Tried with this code and it didn't work:
'.//*[@id="mydiv"]/p[preceding-sibling::p[4] and following-sibling::p[2]]'
You can try:
'//*[@id='mydiv']/p[position()>1 and position()<5]'
Or, your initial code can be changed to:
'//*[@id="mydiv"]/p[preceding-sibling::p and following-sibling::p]'
So that all of the p
with preceding and following p
nodes will be selected (i.e. p[2] through p[4]).
XPATH can be written in below format too:-
//p[position()>1 and position()<5]
OR
//p[position()>=2 and position()<=4]
select me 1
select me 2
select me 3
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