Xpath以获取带有href标记中匹配文本的第二个url [英] Xpath to get the 2nd url with the matching text in the href tag
问题描述
一个html页面具有分页链接,其中一个设置在页面顶部,另一个设置在页面底部.
A html page has paging links, 1 set at the top of the page and another on the bottom of the page.
使用HtmlUnit,我目前正在使用 getByAnchorText("1");
Using HtmlUnit, I am currently getting the HtmlAnchor on the page using getByAnchorText("1");
顶部的某些链接中有问题,因此我想使用XPath引用底部的链接.
There is a problem in some of the links on the top, so I want to reference the bottom links using XPath.
nextPageAnchor = (HtmlAnchor) page.getByXPath("");
如何使用xpath引用页面上的第二个链接?
How can I reference the 2nd link on the page, with using xpath?
我需要使用AnchorText来引用链接,因此链接如下:
I need to reference the link using the AnchorText, so a link like:
<a href="....">33</a>
href具有随机文本,并且是javascript函数,因此我不知道它会是什么.
The href has random text, and is a javascript function so I have no idea what it will be.
xpath可能吗?
推荐答案
要在文档中的任意位置选择第二个 a
元素:
To select the second a
element anywhere in the document:
(//a)[2]
要在 href
属性中选择带有特定文本的第二个 a
元素,请执行以下操作:
To select the second a
element with a particular text in the href
attribute:
(//a[@href='...'])[2]
请注意,需要使用括号,并且表达式//a [2]
不会满足您的期望:它将选择所有 a
元素任何父级的第二个 a
元素.如果您输入的是
Note that the parantheses are required, and that the expression //a[2]
will not do what you intend: it will select all a
elements that are the second a
element of any parent. If your input is
<p>Link <a href="one.html">One</a></p>
<p>Link <a href="two.html">Two</a> and <a href="three.html">Three</a>.</p>
<p>Link <a href="four.html">Four</a> and <a href="five.html">Five</a>.</p>
(//a)[2]
将返回第二个链接(two.html),而//a [2]
将返回第三和第五个链接(three.html和Five.html),因为它们都是父级的第二个 a
子级.
(//a)[2]
will return the second link (two.html), while //a[2]
will return the third and fifth link (three.html and five.html), since these both are the second a
child of their parent.
这篇关于Xpath以获取带有href标记中匹配文本的第二个url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!