HtmlUnit exception

I find it hard to understand the meaning of this HTMLUnit exception. This happens when I call click () on a link on a web page.

Exception class=[net.sourceforge.htmlunit.corejs.javascript.WrappedException] com.gargoylesoftware.htmlunit.ScriptException: Wrapped com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot read property "offsetWidth" from null (http://webapps6.doc.state.nc.us/opi/scripts/DHTMLmessages.js#95) (javascript url#297) at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:534) at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:537) at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:538) at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:432) at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:407) at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptIfPossible(HtmlPage.java:965) at com.gargoylesoftware.htmlunit.html.HtmlAnchor.doClickAction(HtmlAnchor.java:87) at com.gargoylesoftware.htmlunit.html.HtmlAnchor.doClickAction(HtmlAnchor.java:121) at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1329) at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1288) at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1257) at testapp.TestApp.main(TestApp.java:61) Caused by: net.sourceforge.htmlunit.corejs.javascript.WrappedException: Wrapped com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot read property "offsetWidth" from null (http://webapps6.doc.state.nc.us.js#95) (javascript url#297) at net.sourceforge.htmlunit.corejs.javascript.Context.throwAsScriptRuntimeEx(Context.java:1802) at net.sourceforge.htmlunit.corejs.javascript.MemberBox.invoke(MemberBox.java:196) at net.sourceforge.htmlunit.corejs.javascript.FunctionObject.call(FunctionObject.java:479) at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1701) at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:854) at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:164) at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:429) at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:267) at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3183) at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.exec(InterpretedFunction.java:175) at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$5.doRun(JavaScriptEngine.java:423) at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:528) ... 11 more 
+4
source share
3 answers

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

Those don't need Javascript problems with HtmlUnit.

As I said here, HtmlUnit does not create an HtmlPage object , you can set or modify htmlUnit to prevent unnecessary logs. You can also configure log4j and disable some exceptions.

So, we use these options to support html-nawgating, not stopping at the first error / problem that we use:

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

You can also implement empty classes to stop htmlUnity go verbose on the css / javaScript error console using:

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

A small test example:

 @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()); } 

You can also see here Disabling HtmlUnit alerts

+6
source

JS issues with HtmlUnit again. You will need to fix your JS code, as it is very likely that it contains errors in it. If you do not own JS code, then HtmlUnit will not solve this problem. Take a look at the answer I provided here .

+1
source

Htmlunit usually does not handle js code very well. If you want to click on a link, it is better to find the element using the htmlunit api, for example.

  page.getHtmlElementById("some_div_id"); page.getByXPath("//div"); 
0
source

Source: https://habr.com/ru/post/1438325/


All Articles