HtmlUnit Exception

老子叫甜甜 提交于 2019-12-05 12:34:13
Adrien Louis Combecau

When you click and this link call another page, htmlUnit can be very verbose on exceptions during navigation. If you click on your browser and open the console, probably will see those errors, missing links or images, errors on calling scripts..

Those are not necessary Javascript issues with HtmlUnit.

Like I said here HtmlUnit not creating HtmlPage object you can set or modify htmlUnit to prevent unecessary logs. You can also setup log4j and disable some exceptions.

So we use those options to keep html navegating without stoping on first error/problem we use :

webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);

You can also implement empty classes to stop htmlUnity go verbose on console about css/javaScript errors with:

webClient.setCssErrorHandler(new SilentCssErrorHandler());    
webClient.setJavaScriptErrorListener(new JavaScriptErrorListener(){});

The little sample test case:

@Test
public void TestCall() throws FailingHttpStatusCodeException, MalformedURLException, IOException {      
    WebClient webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getOptions().setUseInsecureSSL(true); //ignore ssl certificate
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    String url = "https://www.wearvr.com/#game_id=game_4";
    HtmlPage myPage = webClient.getPage(url);
    webClient.waitForBackgroundJavaScriptStartingBefore(200);
    webClient.waitForBackgroundJavaScript(20000);
    //do stuff on page ex: myPage.getElementById("main")
    //myPage.asXml() <- tags and elements
    System.out.println(myPage.asText());

} 

Also you can see more here Turning HtmlUnit Warnings off

Mosty Mostacho

JS issues with HtmlUnit again. You'll need to correct your JS code as it is very likely that it contains errors in it. If you don't own the JS code then HtmlUnit won't solve this issue. Take a look at the answer I've provided here.

Htmlunit generally does not handle js code pretty well. If you want to click on a link, it is better to find the element using the htmlunit api, eg.

    page.getHtmlElementById("some_div_id");
    page.getByXPath("//div");
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!