XPath for next / sibling / follow HTML元素?

时间:2022-01-06 20:32:48

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

在以下HTML代码中,我有两个嵌套的 链接:

<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:

我可以很容易地找到第一个 链接的XPath,如下所示:

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

现在我想知道如何在不搜索的情况下找到下一个 ,只需说出类似.next()的内容?

1 个解决方案

#1


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]

#1


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]