xpath - axes方法返回错误的节点

时间:2017-11-02 09:03:22

标签: xpath axes

我注意到使用xpath轴方法有时会返回错误的节点。我有两个例子:

url:“http://demo.guru99.com/v1/

<tr>
   <td align="center">
      <img src="../images/1.gif">
      <img src="../images/3.gif">
      <img src="../images/2.gif">
   </td>
</tr>

我可以通过axis方法“// td // child :: img”选择三个img元素。但是当我使用“// td // follow-sibling :: img”时,它仍然可以返回第二个和第三个img元素。据我所知,孩子和兄弟姐妹是两回事,为什么会这样呢?

url:http://demo.guru99.com/selenium/guru99home/

<div class="rt-grid-12 rt-alpha rt-omega" id="rt-feature">
  <div class="rt-grid-6 ">
    <div class="rt-block">
        <h3>
            Desktop, mobile, and tablet access</h3>
        <ul>
            <li>
                <p>
                    Free android App</p>
            </li>
            <li>
                <p>
                    Download any tutorial for free</p>
            </li>
            <li>
                <p>
                    Watch video tutorials from anywhere&nbsp;</p>
            </li>
        </ul>
        <p>
            <a href="https:/play.google.com/store/apps/details?id=com.vector.guru99&amp;hl=en"><img alt="" src="images/app_google_play(1).png"></a></p>
    </div>
  </div>
  <div class="rt-grid-5 ">
    <div class="rt-block">
        <img src="images/logo_respnsivsite.png"><br>
    </div>      
  </div>    
</div>

在这里,如果我使用“// div [@ id ='rt-feature'和(@ class ='rt-grid-12 rt-alpha rt-omega')] // follow-sibling :: div” ,那些应该是子元素的div元素仍然算作兄弟姐妹

使用“// div [@ id ='rt-feature'和(@ class ='rt-grid-12 rt-alpha rt-omega')] // parent :: div”,自我元素及其子div元素都被视为父元素。

这引起了很多困惑,请帮助我。

1 个答案:

答案 0 :(得分:1)

建议XPath解析器返回错误的节点,而不是你不理解为什么它返回它的作用,是从错误的心态开始。除非您知道XPath解析器不可靠,否则请假设它是正确的并且您的期望是错误的。然后转到规范并研究你所写的表达式的语义。

你会发现

//td//following-sibling::img

的缩写
/descendant-or-self::node()/td/descendant-or-self::node()/following-sibling::img

所以你要求所有td节点的所有后代的所有兄弟姐妹,这正是你得到的。

我遇到的人习惯性地写“//”代替“/”作为一种神奇的仙尘,而不是最不知道它意味着什么。不要这样做:阅读规范。

相关问题