与 / 和 // 一起使用时先前轴的移动行为的问题

问题描述 投票:0回答:1

<div id="moe-osm-pusher" style="display: block !important; height: 0px;">
    <div><p>A44</p></div>
    
</div>

<div id="x">
        <p>A</p>
        <div>
          <div>  <p>A11</p> </div>

            <div>
                <p>A12</p>
                <div>
                    <p>A13</p>
     
                </div>
                <div>
                    <p>A23</p>
                    <div><p>A00</p></div>
                </div>
            </div>
            <div>
                <p>A22</p>
                
            </div>
     </div>   
    </div>

//div[p[contains(text(),'A23')]]// 前面::div
//div[p[包含(text(),'A23')]]/前面::div

我的问题是第一个xpath是否会找到这个

<div><p>A00</p></div>
或不? 我在 http://xpather.com/ 中看到它正在定位 div 但为什么。 Xpath 不是意味着倒退吗?

xpath 的公共输出都是:

  • <div> <p>A13</p></div>

  • <div>  <p>A11</p> </div>

  • <div><p>A44</p></div>

  • <div id="moe-osm-pusher" style="display: block !important; height: 0px;"></div>

没关系。

html xpath
1个回答
0
投票

这个问题让我很困惑。答案就在你看不到的节点中:纯空白文本节点。

表情

//div[p[contains(text(),'A23')]]

选择作为

div
 的父级的 
<p>A23</p>

元素

现在

//preceding::div
的意思是
/descendant-or-self::node()/preceding::div
descendant-or-self
选择多个节点,其中之一是紧跟在
<div><p>A00</p></div>
元素后面的空白文本节点。当您从此空白文本节点沿着前一个轴移动时,结果将包含
<div><p>A00</p></div>
元素。

© www.soinside.com 2019 - 2024. All rights reserved.