Need help whith xpath query

I have this html:

<ul class="page-numbers">
<li><a class="prev page-numbers" href="...">prev</a></li>
<li><a class="page-numbers" href="...">1</a></li>
.....
<li><a class="page-numbers" href="...">20</a></li>
<li><a class="next page-numbers" href="...">next</a></li>
</ul>

Need to select the first and last li that contains an a-element with a number (not with text prev or next) This xpath query returns the first li i need ( the li containing page number 1 ):

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

Having problem whe trying to get last li ( the one that contains page number 20 ). Have tried:

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

and

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

But it does not work. The first returns an empty nodeList, the second returns a nodeList that contains all li with numbers (1-20). Can anyone help please?

Upvotes: 0

Views: 31

Answers (1)

Shok9894
Shok9894

Reputation: 67

I think that this XPath can help you:

//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]

[Please, see how it's work from me][1] [1]: https://i.sstatic.net/XQTJ2.png

Upvotes: 1

Related Questions