如何避免/修复HTMLUnit中的ScriptException错误?

时间:2019-11-25 01:44:00

标签: javascript java htmlunit

我正在尝试访问路由器上的网站,以便可以自动控制鱼缸。当我尝试使用HTML Unit连接到网站时,出现错误:

Nov 24, 2019 7:46:15 PM com.gargoylesoftware.htmlunit.javascript.DefaultJavaScriptErrorListener scriptException
SEVERE: Error during JavaScript execution
======= EXCEPTION START ========
EcmaError: lineNumber=[108] column=[0] lineSource=[null] name=[ReferenceError] sourceName=[script in http://24.92.140.58:80/outsetup.sht from (76, 32) to (120, 10)] message=[ReferenceError: "nsys_translator" is not defined. (script in http://24.92.140.58:80/outsetup.sht from (76, 32) to (120, 10)#108)]
com.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "nsys_translator" is not defined. (script in http://24.92.140.58:80/outsetup.sht from (76, 32) to (120, 10)#108)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:883)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:617)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:534)
    at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.callSecured(HtmlUnitContextFactory.java:336)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:812)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:784)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunction(HtmlPage.java:2542)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunction(HtmlPage.java:2535)
    at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeEventListeners(EventListenersContainer.java:342)
    at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeAtTargetListeners(EventListenersContainer.java:379)
    at com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget.fireEvent(EventTarget.java:171)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.lambda$executeEventHandlersIfNeeded$0(HtmlPage.java:1248)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:617)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:534)
    at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.callSecured(HtmlUnitContextFactory.java:336)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeEventHandlersIfNeeded(HtmlPage.java:1248)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:249)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:541)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:400)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:317)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:469)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:450)
    at AlkOutput.main(AlkOutput.java:43)
Caused by: net.sourceforge.htmlunit.corejs.javascript.EcmaError: ReferenceError: "nsys_translator" is not defined. (script in http://24.92.140.58:80/outsetup.sht from (76, 32) to (120, 10)#108)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4334)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4312)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:4406)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.nameOrFunction(ScriptRuntime.java:2009)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.name(ScriptRuntime.java:1948)
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1752)
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:1010)
    at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:111)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:424)
    at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:322)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3628)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$3.doRun(JavaScriptEngine.java:805)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:868)
    ... 22 more
Enclosed exception: 
net.sourceforge.htmlunit.corejs.javascript.EcmaError: ReferenceError: "nsys_translator" is not defined. (script in http://24.92.140.58:80/outsetup.sht from (76, 32) to (120, 10)#108)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4334)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4312)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:4406)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.nameOrFunction(ScriptRuntime.java:2009)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.name(ScriptRuntime.java:1948)
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1752)
    at script(script in http://24.92.140.58:80/outsetup.sht from (76, 32) to (120, 10):108)
    at script(script in http://24.92.140.58:80/outsetup.sht from (-1, -1) to (-1, -1))
    at script(script in http://24.92.140.58:80/outsetup.sht from (-1, -1) to (-1, -1))
    at script(script in http://24.92.140.58:80/outsetup.sht from (-1, -1) to (-1, -1))
    at script(script in http://24.92.140.58:80/outsetup.sht from (-1, -1) to (-1, -1))
    at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:1010)
    at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:111)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:424)
    at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:322)
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3628)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$3.doRun(JavaScriptEngine.java:805)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:868)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:617)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:534)
    at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.callSecured(HtmlUnitContextFactory.java:336)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:812)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:784)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunction(HtmlPage.java:2542)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunction(HtmlPage.java:2535)
    at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeEventListeners(EventListenersContainer.java:342)
    at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeAtTargetListeners(EventListenersContainer.java:379)
    at com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget.fireEvent(EventTarget.java:171)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.lambda$executeEventHandlersIfNeeded$0(HtmlPage.java:1248)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:617)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:534)
    at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.callSecured(HtmlUnitContextFactory.java:336)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeEventHandlersIfNeeded(HtmlPage.java:1248)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:249)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:541)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:400)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:317)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:469)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:450)
    at AlkOutput.main(AlkOutput.java:43)
== CALLING JAVASCRIPT ==
  function () {
      document.removeEventListener("DOMContentLoaded", arguments.callee, false);
      T.ready();
  }
======= EXCEPTION END ========

我不知道如何绕过此错误,因此我可以访问该网站。我尝试使用命令WebClient.getOptions().setThrowExceptionOnScriptError(false);来忽略该错误,但这似乎无法解决问题。我也尝试过切换BrowserVersion,但这也无济于事。也许问题出在网站上。我无权更改网站上的代码。这是我的代码。

public class AlkOutput {

    public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {

        DefaultCredentialsProvider cred = new DefaultCredentialsProvider();
        cred.addCredentials(username, password); //I didn't include real username / password for privacy

        WebClient web = new WebClient(BrowserVersion.INTERNET_EXPLORER);

        web.getOptions().setThrowExceptionOnScriptError(false);

        web.setCredentialsProvider(cred);

        HtmlPage page = web.getPage("http://24.92.140.58:80/outsetup.sht"); //error is on this line

        web.close();
   }
}

1 个答案:

答案 0 :(得分:0)

请您检查一下真实的浏览器是否发生了相同的错误。

设置WebClient.getOptions()。setThrowExceptionOnScriptError(false);仍然会报告错误,但会继续处理页面中的js(就像浏览器一样)。

为什么您认为它不起作用-您是否获得了预期的页面?