Xpath Get elements that are between 2 elements

2020-02-06 03:45发布

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>

标签: xpath
2条回答
不美不萌又怎样
2楼-- · 2020-02-06 04:04
div[preceding-sibling::h4[1] = 'Two']
查看更多
时光不老,我们不散
3楼-- · 2020-02-06 04:04
div[preceding-sibling::h4='Two' and following-sibling::h4='Three']
查看更多
登录 后发表回答