Xpath Get elements that are between 2 elements

2020-02-06 04:14发布

问题:

can anyone tell me if its possible to select only the divs 2a and 2b from this html fragment? the problem is that the divs are not children of h4 element and so the xpath query should say like "get the divs that are between the h4='Two' and the h4 that is right after h4='Two' note that i want the query to be dynamic and u tell her the start element (h4='Two') and the end element (any h4) and then on which filter to get the elements between.

<h4>One</h4>
<div>1a</div>
<div>1b</div>
<div>1c</div>
<h4>Two</h4>
<div>2a</div>
<div>2b</div>
<h4>Three</h4>
<div>3a</div>
<div>3b</div>
<div>3c</div>

回答1:

div[preceding-sibling::h4[1] = 'Two']


回答2:

div[preceding-sibling::h4='Two' and following-sibling::h4='Three']


标签: xpath