[英]Xpath to find all elements following specific node
I have a specific html as shown below and i had observed that there is one specific h2 node that appears in all cases. 我有一个特定的html,如下所示,并且我观察到在所有情况下都出现一个特定的h2节点。
<div id='myDiv'>
<..other elements..>
<h2>Locator</h2>
<h3>Header 1 </h3>
<p>Paragraph 1.1</p>
<h3>Header 2 </h3>
<p>Paragraph 2.1</p>
<p>Paragraph 2.2.</p>
<p>Paragraph 2.3.</p>
<h4> test header 4</h4>
</div>
I want all the markups after the specific node the html format is as below. 我想要在特定节点后的所有标记,html格式如下。 As you can see i can locate the h2 tag by applying xpath
//div[@id='myDiv']/h2[contains(.,"Locator")]
but the trouble is finding all elements inside the div following the h2 tag.(I am not sure what types of tags can come after this h2 tag).Just to be more clear, What is the xpath to get the following nodes selected 如您所见,我可以通过应用xpath
//div[@id='myDiv']/h2[contains(.,"Locator")]
来定位h2标签,但麻烦的是在h2标签之后的div中查找所有元素(我不确定在此h2标签之后会出现什么类型的标签)。只是更清楚一点,什么是xpath来选择以下节点?
<h3>Header 1 </h3>
<p>Paragraph 1.1</p>
<h3>Header 2 </h3>
<p>Paragraph 2.1</p>
<p>Paragraph 2.2.</p>
<p>Paragraph 2.3.</p>
<h4> test header 4</h4>
*[preceding-sibling::h2]
indicates all elements after h2
. *[preceding-sibling::h2]
表示h2
之后的所有元素。 So after adding contains in the selector it goes like this 所以在选择器中添加包含之后,它就像这样
//div[@id='myDiv']/*[preceding-sibling::h2[contains(text(),'Locator')]]
See Demo Here . 请参阅此处的演示 。 Click The Test button you will get the result
单击测试按钮,您将获得结果
You appear to be looking for the following-sibling
axis: 您似乎正在寻找
following-sibling
轴:
//div[@id='myDiv']/h2[contains(.,"Locator")]/following-sibling::*
Note, however, that the following siblings are, indeed, siblings of the nodes selected by the previous step in the path. 但是请注意,以下同级确实是路径中上一步选择的节点的同级 。 In your example, that's exactly what all the elements you want to select are (with respect to the
h2
node), but if any of those nodes had child elements, those child elements would not be siblings to the h2
, and would not be selected. 在您的示例中,这就是您要选择的所有元素的确切位置(相对于
h2
节点),但是如果这些节点中的任何一个具有子元素,则这些子元素将不会是h2
兄弟元素,也不会被选择。
If you want all those child elements as well, and their children, etc., then you could use the descendant-or-self
axis, too: 如果还需要所有这些子元素及其子元素等,则也可以使用
descendant-or-self
轴:
//div[@id='myDiv']/h2[contains(.,"Locator")]/following-sibling::*/descendant-or-self::*
我会将//h2[contains(text(),"Locator")]/following-sibling::*
用于嵌套在与h2相同父级下但比h2“低”的所有标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.