<p class="small" style="margin: 16px 4px 8px;"> <b> <a class="menu-root" href="#pg-jump">Pages</a> : <b>1</b> , <a class="pg" href="viewforum.php?f=941&start=50">2</a> , <a class="pg" href="viewforum.php?f=941&start=100">3</a> ... <a class="pg" href="viewforum.php?f=941&start=8400">169</a> , <a class="pg" href="viewforum.php?f=941&start=8450">170</a> , <a class="pg" href="viewforum.php?f=941&start=8500">171</a> <a class="pg" href="viewforum.php?f=941&start=50">Next.</a> </b> </p>
I want to catch a
element containing 171. So basically the preceding element from the Next.
//a[.='Next.']//Not sure how to use preceding here
Preceding Axis: Now we will select all nodes by using the preceding axis in the document that comes before the current node. XPath: //input[@id = 'u_0_2']//preceding::input (1 of 4 matched). The above expression identified all the input elements before “login” button using the preceding axis.
The following-sibling and preceding-sibling axes contain the siblings before or after the context node, and the following and preceding axes contain all nodes in the document before or after the context node, but: None of these axes contain attribute or namespace nodes.
In this diagram, the preceding:: nodes are circled, and the more specific preceding-sibling:: nodes are given a red border. The preceding-siblings are nodes that share the same parent node, and come before the current node in the document. The preceding:: node is any node that comes before the element in the document.
You can use this xpath
:
//a[.="Next."]/preceding::a[1]
If I were to diagram it out, using an X
to represent the current location, it would look like this:
------------------+------+------------------ preceding-sibling | self | following-sibling ------------------|------|------------------ last() ... 2 1 | X | 1 2 ... last() ------------------+------+------------------
//a[contains(text(), 'Next.')]/preceding::a[contains(text(), '171')]
Explanation of xpath: Using text
method along with <a>
tag and then move ahead with preceding
keyword to locate the element 171
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