【问题标题】:XPath for text based on content in preceding element?基于前一个元素中的内容的文本的XPath?
【发布时间】:2019-04-05 13:17:27
【问题描述】:
<div class="profile-row">

        <div class="profile-cell">

            <h4 class="">Telephone</h4>

        </div>

        <div class="profile-cell">

            <p class="">0207 289 2981</p>

        </div>

    </div>

我正在尝试获取电话号码:0207 289 2981

使用以下变体:

//h4[starts-with(., 'Telephone')]/following-sibling::div[@class='profile-cell']

和:

//h4[starts-with(., 'Telephone')]/following-sibling::div/p

似乎无法抓住这个。

【问题讨论】:

    标签: html xml xpath


    【解决方案1】:

    兄弟姐妹有共同的父母; h4 和 p 没有。

    请改用following::。

    这个 XPath,

    //h4[.='Telephone']/following::p[1]/text()
    

    将从您的目标h4 中选择紧随其后的p 的文本。

    【讨论】:

      【解决方案2】:

      这里是 xpath。

      (//div[@class='profile-cell']//p)[last()]
      

      xpath 中的问题是 h4 没有同级 div。所以你必须访问父 div,然后选择兄弟 div,如下所示。

      //h4[starts-with(., 'Telephone')]/parent::div/following-sibling::div[@class='profile-cell']/p
      

      【讨论】:

        【解决方案3】:

        这会选择实际数字:

        //p[@class='']/text()
        

        【讨论】:

          猜你喜欢
          • 2015-12-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-10
          • 2013-01-21
          • 2015-11-06
          • 2013-01-15
          • 1970-01-01
          相关资源
          最近更新 更多