Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath for next/sibling/following HTML element?

In the following HTML code, I have two nested <href> links:

<a href="/cgi-bin/WebOb/mamool/8.2">
<img width="12" border="0" align="ABSMDIDDLE" height="7" src="/WebOb/mamool/Frameworks/fig.gif">
Click me for more info
</a>
<table border="1" size="2" font="">
<tbody>
<tr>
<td>
<font size="2">
<a name="179"></a>
    <a href="/cgi-bin/WebOb/mamool/8.2.44">
    <img width="12" border="0" align="ABSMDIDDLE" height="7" src="/WebOb/mamool/Frameworks/myfig.gif">
</a>
</td>
</tr>
<tr bgcolor="#d6e2ff">
<td>
</tr>
</tbody>
</table>

I can easily find the XPath for the first <href> link like this:

//a[contains(text(), 'Click me for more info')]

Now I am wondering how to find the next <href> without searching, just say something like .next() ?

like image 237
Mohammad Avatar asked Oct 26 '25 08:10

Mohammad


1 Answers

The next sibling element can be selected using the following-sibling axis:

//a[contains(text(), 'Click me for more info')]/following-sibling::*[1]

would select the table element in your example.

If you want to select the next a element in the document, use the following axis:

//a[contains(text(), 'Click me for more info')]/following::a[1]
like image 107
kjhughes Avatar answered Oct 28 '25 22:10

kjhughes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!