The following html shows "ok!" in browsers, but exceptions caught by htmlunit。
html code:
<html><head><title>test</title></head>
<body>
<div id="target">origin</div>
<script type="text/javascript">
var a = {};
a.prototype = {
foo : foo,
};
var container = {
foo : function foo() {
}
};
var foo = container.foo;
document.getElementById("target").textContent="ok!";
</script>
</body></html>
Java code:
public static void main(String[] args) throws Exception
{
String link = "file:test_001.html";
try (WebClient webClient = new WebClient(BrowserVersion.BEST_SUPPORTED))
{
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
HtmlPage page = webClient.getPage(link);
webClient.waitForBackgroundJavaScriptStartingBefore(800);
webClient.waitForBackgroundJavaScript(800);
System.out.println(page.getElementById("target").getTextContent()); // should be ok!
}
} |