【问题标题】:Xpath for finding element that does not have sibling用于查找没有兄弟元素的 Xpath
【发布时间】:2015-01-27 20:39:10
【问题描述】:

所以我试图从下面的链接中提取 a href 位置,但仅适用于没有跨度兄弟的 a 元素。我已经设法找出 Xpath 来找到那些做的,但我该如何否定呢?我试过使用 count() 和 not() 但似乎无法让它们有效或返回任何东西。

<div class="member">
    <a href="/test1" class="ahrefclass">shouldntfindme</a>
    <span class="evilspan">evilspan</span>
</div>
<div class="member">
    <a href="/test2" class="ahrefclass">findme</a>
</div>
<div class="member">
    <a href="/test3" class="ahrefclass">shouldntfindme</a>
    <span class="evilspan">evilspan</span>
</div>
<div class="member">
    <a href="/test4" class="ahrefclass">findme</a>
</div>
<div class="member">
    <a href="/test5" class="ahrefclass">shouldntfindme</a>
    <span class="evilspan">evilspan</span>
</div>

到目前为止,我的 Xpath 是

//a[@class="ahrefclass"][../span[@class="evilspan"]]

【问题讨论】:

    标签: html dom xpath xpath-2.0


    【解决方案1】:

    只需否定条件:

    //a[@class="ahrefclass"][not(../span[@class="evilspan"])]
    

    在这种情况下,相当于

    //a[@class="ahrefclass" and not(../span[@class="evilspan"])]
    

    【讨论】:

    • [x][y][x and y] 之间存在差异。见stackoverflow.com/a/1006439/18771
    • 啊,谢谢!我确实尝试过不使用,不确定为什么我没有运气,一定是在错误的位置使用它或其他什么:-)
    • @Tomalak:在这种情况下,它们是等价的。
    • 在这种情况下是的。这并不是一种更正,而是一种补充。
    • 简单一点(在我看来)是//div[not(span/@class='evilspan')]/a[@class='ahrefclass']
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    相关资源
    最近更新 更多