如何避免/修复 HTMLUnit 中的脚本异常错误?



>我正在尝试访问路由器上的网站,以便我可以自动控制鱼缸。当我尝试使用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();
}
}

您能否检查一下真正的浏览器是否会发生相同的错误。

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

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

根据我的经验,带有Java脚本错误的HtmlUnit的默认行为一直是一团糟,因为在包含大量脚本库时,期望在互联网上找到一个页面而没有错误,这是非常小的。然后,用异常处理任何错误,本质上会降低对HTMLunit的接受度。

对我来说,常见的解决方案是:

WebClient cl =  = new WebClient(BrowserVersion.FIREFOX); // or similair
cl.setCssErrorHandler(...);
cl.setIncorrectnessListener(...);
cl.setJavaScriptErrorListener(...);
cl.getOptions().setThrowExceptionOnScriptError(false);

其中...将替换为专用接口的实现。

最新更新