【问题标题】:HtmlUnit ExceptionHtmlUnit 异常
【发布时间】:2016-10-20 15:48:25
【问题描述】:

我无法理解这个 HTMLUnit 异常的含义。当我在网页上的链接上调用 click() 时会发生这种情况。

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

【问题讨论】:

  • 从堆栈跟踪:包装 com.gargoylesoftware.htmlunit.ScriptException:TypeError:无法从 null 读取属性“offsetWidth”
  • 添加 log4j 作为依赖项以阻止 HtmlUnit 打印这些消息。

标签: java htmlunit


【解决方案1】:

当您单击此链接并调用另一个页面时,htmlUnit 在导航期间的异常可能会非常冗长。如果您单击浏览器并打开控制台,可能会看到这些错误、缺少链接或图像、调用脚本时出错..

这些不是 HtmlUnit 的必要 Javascript 问题。

就像我在这里HtmlUnit not creating HtmlPage object 说的那样,您可以设置或修改 htmlUnit 以防止不必要的日志。您还可以设置 log4j 并禁用一些异常。

所以我们使用这些选项来保持 html 导航,而不会停止我们使用的第一个错误/问题:

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

您还可以实现空类来阻止 htmlUnity 在控制台上显示关于 css/javaScript 错误的详细信息:

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

小样本测试用例:

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

} 

你也可以在这里看到更多Turning HtmlUnit Warnings off

【讨论】:

    【解决方案2】:

    JS 再次出现 HtmlUnit 问题。您需要更正您的 JS 代码,因为它很可能包含错误。如果您不拥有 JS 代码,那么 HtmlUnit 将无法解决此问题。看看我提供的答案here

    【讨论】:

      【解决方案3】:

      Htmlunit 通常不能很好地处理 js 代码。 如果要点击链接,最好使用 htmlunit api 查找元素, 例如。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-16
        • 2013-01-13
        • 1970-01-01
        • 2013-03-28
        • 1970-01-01
        相关资源
        最近更新 更多