使用htmlunit(Java)单击链接

时间:2016-02-15 12:03:56

标签: java dom htmlunit

我正试图让htmlunit点击页面中的第二个链接(Contacts):

<ul role="tablist">
<li class="nav-item nav-item-game active" data-nav="game" title="Game" role="presentation">
<a class="nav-lnk" aria-selected="true" role="tab" tabindex="0">
<span class="offscreen">Game</span>
<b class="icon icon-game"></b>
</a>
</li>
<li class="nav-item nav-item-contacts" data-nav="contacts" title="Contacts" role="presentation">
<a class="nav-lnk" aria-selected="false" role="tab" tabindex="-1">
<span class="offscreen">Contacts</span>
<b class="icon icon-contacts"></b>
</a>
</li>
</ul>

现在我正在检索列表中的所有链接:

List<DomNode> contactTabs = (List<DomNode>) gameLoggedInPage.getByXPath("//a[@class='nav-lnk']/)");
DomNode contacts = contactTabs.get(1);

如何点击htmlunit的第二个链接?

1 个答案:

答案 0 :(得分:1)

试试这个:

List<HtmlAnchor> contactTabs = (List<HtmlAnchor>) gameLoggedInPage.getByXPath("//a[@class='nav-lnk'])");
HtmlAnchor contacts = contactTabs.get(1);
HtmlPage page = contacts.click();