【问题标题】:Xpath find a div without children with specific textXpath 找到一个没有特定文本的子元素的 div
【发布时间】:2017-11-16 09:37:18
【问题描述】:

我需要使用给定文本检索没有子级的 div。我有这个html

<h1>Rest Object</h1>
<div style="background-color: transparent;">
    <div>Title: Rest object</div>
    <div>ID: 2</div>
    <div>Title: Rest object Copy</div>
    <div>Full text: This is the full text. ID: 2</div>
    <div>Value: 0.564</div>
    <div>Timestamp: 2017-06-14 11:35:40</div>
</div>

我想找到&lt;div&gt;ID: 2&lt;/div&gt;。如何?我试过了

xpath=(//div)

它返回第一个 div。我尝试使用

xpath=(//div[not(div)])

然后它返回 &lt;div&gt;Title: Rest object&lt;/div&gt;.



更新。现在我知道我可以为您编制索引。

 xpath=(//div[not(div)][2])

&lt;div&gt;ID: 2&lt;/div&gt;.

如果我不知道索引怎么办。

返回

【问题讨论】:

    标签: xpath html-parsing


    【解决方案1】:

    获得所需div 的一种方法是使用starts-with() 函数:

    //div[starts-with(.,'ID:')]    
    

    boolean starts-with(string, string) - 如果第一个参数 string 以第二个参数 string 开头,则返回 true;否则返回false


    要将搜索限制在没有子元素的div 元素,您可以使用count(*) 函数:

    //div[starts-with(.,'ID:')][count(*)=0]
    

    【讨论】:

    • 是的,它有效! xpath=(//div[starts-with(.,'ID')])。如果我想要一个没有孩子的 div 怎么办?
    • @YanKhonski,试试[not(./*)]谓词
    猜你喜欢
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 2020-08-20
    相关资源
    最近更新 更多