问题描述
我有一个基于 GWT 的页面,我想使用 HtmlUnit 为其创建 HTML 快照.该页面使用有关产品的 Ajax/JavaScript 信息加载,因此大约 1 秒钟内会显示一条正在加载...消息,然后显示内容.
I have a GWT based page that I would like to create an HTML snapshot for it using HtmlUnit.The page loads using Ajax/JavaScript information on a product, so for about 1 second there is a Loading... message and then the content appears.
问题是 HtmlUnit 似乎没有捕获信息,而我得到的只是正在加载..."范围.
The problem is that HtmlUnit doesn't seem to capture the information and all I'm getting is the "Loading..." span.
下面是一个带有 HtmlUnit 的实验代码,我尝试给它足够的时间来等待数据的加载,但它似乎没有改变任何东西,我仍然无法捕获 GWT javascript 加载的数据.
Below is an experimental code with HtmlUnit where I try to give it enough time to wait for the loading of the data but it doesn't seem to change anything and I am still unable to capture the data loaded by the GWT javascript.
WebClient webClient = new WebClient();
webClient.setJavaScriptEnabled(true);
webClient.setThrowExceptionOnScriptError(false);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
WebRequest request = new WebRequest(new URL("<my_url>"));
HtmlPage page = webClient.getPage(request);
int i = webClient.waitForBackgroundJavaScript(1000);
while (i > 0)
{
i = webClient.waitForBackgroundJavaScript(1000);
if (i == 0)
{
break;
}
synchronized (page)
{
System.out.println("wait");
page.wait(500);
}
}
webClient.getAjaxController().processSynchron(page, request, false);
System.out.println(page.asXml());
有什么想法...?
推荐答案
感谢您的回复.我实际上应该早点报告这个问题,因为我自己找到了解决方案.显然在使用 FF 初始化 WebClient 时:
Thank you for responding.I actually should have reported this sooner that I have found the solution myself.Apparently when initialising WebClient with FF:
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
它似乎起作用了.当使用默认构造函数初始化 WebClient 时,它默认使用 IE7,我猜 FF 对 Ajax 有更好的支持,是推荐使用的模拟器.
It seem to be working.When initialising WebClient with the default constructor it uses IE7 by default and I guess FF has better support for Ajax and is the recommended emulator to use.
这篇关于HTMLUnit 不等待 Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!