基于前面元素内容的XPath for text?

时间:2019-04-05 13:17:27

标签: html xml xpath

<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

似乎无法抓住这个机会。

3 个答案:

答案 0 :(得分:0)

这是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

答案 1 :(得分:0)

兄弟姐妹有一个共同的父母; h4p不会。

改为使用following::

此XPath,

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

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

答案 2 :(得分:0)

这将选择实际数字:

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