【问题标题】:Need help whith xpath query需要有关 xpath 查询的帮助
【发布时间】:2017-09-17 17:42:48
【问题描述】:

我有这个 html:

需要选择第一个和最后一个包含带有数字的a元素的li(不是带有文本prev或next) 此 xpath 查询返回我需要的第一个 li(包含页码 1 的 li):

    //ul[ @class='page-numbers' ]
    /li[
        not(
            a[ contains( @class, 'prev' ) ]
        ) and
        not(
            a[ contains( @class, 'next' ) ]
        ) and
        position() = 1
    ]"

在尝试获取最后一个 li(包含页码 20 的那个)时遇到问题。试过了:

    //ul[ @class='page-numbers' ]
    /li[
        not(
            a[ contains( @class, 'prev' ) ]
        ) and
        not(
            a[ contains( @class, 'next' ) ]
        ) and
        position() = last()
    ]"

    //ul[ @class='page-numbers' ]
    /li[
        not(
            a[ contains( @class, 'prev' ) ]
        ) and
        not(
            a[ contains( @class, 'next' ) ]
        ) and
        last()
    ]"

但它不起作用。第一个返回一个空节点列表,第二个返回一个节点列表,其中包含所有带有数字 (1-20) 的 li。 有人可以帮忙吗?

【问题讨论】:

    标签: xpath


    【解决方案1】:

    我认为这个 XPath 可以帮助你:

    //ul/li[not(./a[contains(text(),'next')]) and not(./a[contains(text(),'prev')])][last()] | //ul/li[not(./a[contains(text(),'next')]) and not(./a[contains(text(),'prev')])][1]

    [请看我的工作原理][1] [1]:https://i.stack.imgur.com/XQTJ2.png

    【讨论】:

    • 稍作修改即可完美运行:[@class='page-numbers']。 //ul[@class='page-numbers']/li[not(a[ contains(@class,'prev')]) and not(a[contains(@class,'next')] ) ][ 最后的() ]。非常感谢!
    猜你喜欢
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 2017-08-02
    • 1970-01-01
    相关资源
    最近更新 更多