简体   繁体   English

Xpath查找特定节点之后的所有元素

[英]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“低”的所有标记。

XPath 查询:如何找到所有<div>有 2<a>作为前 2 个元素?</a></div><div id="text_translate"><p> <a>XPath 查询:如何找到所有具有 2 个&lt;a&gt;作为前 2 个元素的&lt;div&gt; ?</a></p><p> 例如,要查找所有&lt;div&gt; ,使用$xpath-&gt;query(); 在哪里:</p><pre> &lt;div&gt; &lt;a href="https://www.somesite.com/" id="" src="" alt="" /&gt;&lt;/a&gt; &lt;a href="https://www.somesite.com/" id="" style=""&gt;&lt;/a&gt;... more elements of various kinds... &lt;/div&gt;... more elements of various kinds...</pre><p> 任何帮助将不胜感激。</p></div> - XPath query: How to find all the <div> that have 2 <a> as the first 2 elements?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何查找特定节点的 xpath 元素/标签 - How to find xpath elements/tags of a specific node 使用 XPath 查找最上面的元素 - Find top-most following elements with XPath 没有特定类后代的所有元素的XPath? - XPath for all elements without descendants of specific class? 具有特定值的任何属性的所有元素的 XPath? - XPath for all elements with any attribute with specific value? Xpath python在特定文本后找到节点 - Xpath python find node after specific text XPath:选择以下所有节点,直到某个节点为止 - XPath: Select all following nodes until some node 文档中所有没有文本节点后代的元素的Xpath? - Xpath for all elements in the document without text node descendants? XPath 查询:如何找到所有<div>有 2<a>作为前 2 个元素?</a></div><div id="text_translate"><p> <a>XPath 查询:如何找到所有具有 2 个&lt;a&gt;作为前 2 个元素的&lt;div&gt; ?</a></p><p> 例如,要查找所有&lt;div&gt; ,使用$xpath-&gt;query(); 在哪里:</p><pre> &lt;div&gt; &lt;a href="https://www.somesite.com/" id="" src="" alt="" /&gt;&lt;/a&gt; &lt;a href="https://www.somesite.com/" id="" style=""&gt;&lt;/a&gt;... more elements of various kinds... &lt;/div&gt;... more elements of various kinds...</pre><p> 任何帮助将不胜感激。</p></div> - XPath query: How to find all the <div> that have 2 <a> as the first 2 elements? Xpath所有后续兄弟姐妹 - Xpath all following-siblings XPath查找元素 - XPath to find elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM