HtmlUnit中未触发Javascript onload事件 [英] Javascript onload event not firing in HtmlUnit
本文介绍了HtmlUnit中未触发Javascript onload事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从HTML字符串而不是从服务器加载页面.
I need to load a page from an HTML string, not from a server.
我使用此答案中找到的方法:
I use the method found in this answer :
String html = "<html><head><script>" +
"alert('hey 1'); " +
"function OnLoadEvent() { alert('hey 2'); }" +
"</script></head>" +
"<body onload='OnLoadEvent()'></body></html>"
URL url = new URL("http://www.example.com");
StringWebResponse response = new StringWebResponse(html, url);
WebClient client = new WebClient()
client.getOptions().setJavaScriptEnabled(true);
HtmlPage page = HTMLParser.parseHtml(response, client.getCurrentWindow());
我还设置了AlertHandler
,如下所示:
I have also set an AlertHandler
as follows:
client.setAlertHandler(new AlertHandler() {
@Override
public void handleAlert(Page arg0, String arg1) {
System.out.println("ALERT: " + arg1);
}
});
上面的示例HTML仅警告嘿1"-我希望它能够警告嘿1"和嘿2"-这意味着正文onload
事件未触发.
The example HTML above alerts "hey 1" only -- I would expect it to alert "hey 1" and "hey 2" -- this implies the body onload
event is not firing.
要触发onload
事件,我还需要做些其他事情吗?
Is there something extra I need to do to make the onload
event fire?
推荐答案
我错过了对HtmlPage.initialize()
的呼叫.
这篇关于HtmlUnit中未触发Javascript onload事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文