Mohammad
Mohammad

Reputation: 1108

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() ?

Upvotes: 4

Views: 935

Answers (1)

kjhughes
kjhughes

Reputation: 111686

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]

Upvotes: 4

Related Questions