4

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

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Mohammad
  • 1,078
  • 2
  • 18
  • 39

1 Answers1

3

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]
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • just a quick qestion @kjhughes, how about if I have two href links which both contain 'Click me for more info', how should I select the first one, is this correct? //a[contains(text(), 'Click me for more info')][1]/following::a[1] – Mohammad May 05 '15 at 00:59
  • 1
    To select a second "Click me for more info" `a`, use `//a[contains(text(), 'Click me for more info')][2]`. – kjhughes May 05 '15 at 01:03
  • Hi @kjhughes , I just have a question on this. Is there anyway to make the "contains" function independent from capital or small letters. For example in the above case I want to detect both "Click me for more info" and also "cLicK ME for more Info". Thanks a lot – Mohammad May 06 '15 at 18:29
  • 1
    Yes: See [XPath 2.0 case-insensitive contains()](http://stackoverflow.com/a/23388974/290085) or, if you're stuck with XPath 1.0 see [here](http://stackoverflow.com/a/8474552/290085) . – kjhughes May 06 '15 at 18:47