单击使用htmlunit的javascript锚点似乎不起作用 [英] Clicking on javascript anchor with htmlunit does not seem to work
问题描述
我正在使用htmlunit自动浏览网站。
以下是问题:
I am using htmlunit to automatically go through a website. Here is the problem:
我想点击锚点以显示给定表格的新页面。
I want to click on an anchor in order to display a new page of a given table.
以下是锚点:
<a href="javascript:__doPostBack('GridView1','Page$7')">7</a>
这是我的代码:
final HtmlAnchor a = page2.getAnchorByText("7");
HtmlPage page3 = a.click();
System.out.println(page2.getWebResponse().getContentAsString())
System.out.println(page3.getWebResponse().getContentAsString());
我没有任何错误消息。当我比较我的打印时,它们是相同的,但它们不应该是,因为我只是点击一个锚点。首先打印输出应该显示表的某个页面,第二个打印输出另一个页面。
I do not have any error message. When I compare my print out, they are identical, and yet they shouldn't be, as I just clicked on an anchor. First print out should display a certain page of the table, a the second print out another one.
stackoverflow post 努力点击htmlunit中的链接会产生一个非常类似的问题,但他的解决方案(将浏览器版本设置为webclient)确实存在问题在我的情况下似乎不起作用。
The stackoverflow post struggling to click on link within htmlunit renders a very similar problem, however his solution (setting the browser version to the webclient) does not appear to work in my case.
推荐答案
也许你需要等待javascript先执行并加载。我建议使用以下命令:
Maybe you need to wait for the javascript to excute and load first. I suggest to use the following command:
webClient.waitForBackgroundJavaScript(1000);
甚至:
JavaScriptJobManager manager = page.getEnclosingWindow().getJobManager();
while (manager.getJobCount() > 0) {
Thread.sleep(100);
}
还有另一种选择:
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.setAjaxController(new AjaxController(){
@Override
public boolean processSynchron(HtmlPage page, WebRequest request, boolean async)
{
return true;
}
});
这篇关于单击使用htmlunit的javascript锚点似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!