【问题标题】:How to avoid / fix ScriptException error in HTMLUnit?如何避免/修复 HTMLUnit 中的 ScriptException 错误?
【发布时间】:2019-11-25 01:44:00
【问题描述】:

我正在尝试访问路由器上的网站,以便自动控制鱼缸。当我尝试使用 HTML 单元连接到网站时,它给了我一个错误:

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

【问题讨论】:

标签: javascript java htmlunit


【解决方案1】:

能否请您检查一下在真正的浏览器上是否出现同样的错误。

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

为什么你认为它不起作用 - 你得到预期的页面吗?

【讨论】:

  • 当我在 chrome 中检查网站时,错误似乎仍然存在。我读过真正的浏览器可以忽略这些错误并且仍然可以工作,但是 HTML 单元很难忽略这些错误。当我在真正的浏览器上加载页面时,页面工作正常。我的目标是在网站上的文本框中输入命令,并且可以在真正的浏览器中使用。我想在无头浏览器上执行此操作以最大程度地减少负载。
猜你喜欢
  • 1970-01-01
  • 2013-12-17
  • 2019-08-15
  • 1970-01-01
  • 2015-11-18
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多