A html page has paging links, 1 set at the top of the page and another on the bottom of the page.
Using HtmlUnit, I am currently getting the HtmlAnchor on the page using getByAnchorText("1");
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("");
How can I reference the 2nd link on the page, with using xpath?
I need to reference the link using the AnchorText, so a link like:
<a href="....">33</a>
The href has random text, and is a javascript function so I have no idea what it will be.
Is this possible with xpath?
To select the second a
element anywhere in the document:
(//a)[2]
To select the second a
element with a particular text in the href
attribute:
(//a[@href='...'])[2]
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]
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.