I have this html
<a class=pagination_klass></a>
<a class=pagination_klass></a>
<a class=pagination_klass></a>
<a class=pagination_klass></a>
<a class=pagination_klass>HERE</a>
<a class=pagination_klass></a>
I want to get the before last <a>
I tried this:
.//a[@class='pagination_klass' and position() = (last()-1)]/@href
but I got empty results.
help please.
I need to compare on the class name too
Using XPath- last() method, we can write the Java code along with the dynamic XPath location as: findElement(By. xpath("(//input[@type='text'])[last()]"))
You can use the last() feature of xpath.
The syntax for locating elements through XPath- Using contains() method can be written as: //<HTML tag>[contains(@attribute_name,'attribute_value')]
Hi you got it almost correct. I removed . selector (current node selector) at beginning of XPath and I test it here on Xpath tester. It works fine for me.
//a[@class='pagination_klass' and position() = (last()-1)]/@href
For
<html>
...
<a class='pagination_klass'></a>
<a class='pagination_klass'></a>
<a class='pagination_klass'></a>
<a class='pagination_klass'></a>
<a class='pagination_klass' href='LINK'>HERE</a>
<a class='pagination_klass'></a>
..
</html>
will be result attribute node href='LINK'.
Your expression
.//a[@class='pagination_klass' and position() = (last()-1)]/@href
will select the second-to-last of all links but only if its class equals pagination_klass
. If you want to find the second-to-last of all pagination_klass
links, try:
.//a[@class='pagination_klass'][last()-1]/@href
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With