From: 蒲云 <cl...@12...> - 2016-07-10 04:09:44
|
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"> try{ getComputedStyle(void 0); }catch(e){}; document.getElementById("target").textContent="ok!"; </script> </body> </html> Java code: public static void main(String[] args) throws Exception { String link = "file:test_002.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! } } |