java-Htmlunit ScriptException“控制台”未定义
作者:互联网
我正在使用htmlunit 2.9并在Java脚本解析中由于以下异常中的控制台而导致脚本异常
function debug(o){
if (console && console.log){
console.log(o)
}
};
堆栈跟踪
EcmaError:
lineNumber=[168]
column=[0]
lineSource=[null]
name=[ReferenceError]
sourceName=[script in http://localhost:808/mypage/ll.html from (154, 36) to (301, 14)]
message=[ReferenceError: "console" is not defined. (script in http://localhost:8080.com/mypage/ll.html from (154, 36) to (301, 14)#168)]
com.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "console" is not defined. (script in http://localhost:8080.com/mypage/ll.html from (154, 36) to (301, 14)#168)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:595)
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.callFunction(JavaScriptEngine.java:545)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:520)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:896)
at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeEventHandler(EventListenersContainer.java:195)
at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:214)
如果我尝试在firefox上指定的页面工作正常,那么我已经尝试了v 3.6和9.0.1.
我也尝试设置setThrowExceptionOnScriptError(false)以避免异常,但引擎停止运行,或者在收到错误后不解析javascript.
javascript引擎可以通过任何方式理解javascript中的控制台吗?
解决方法:
您的if条件结构不正确:
if (console && console.log){
如果未设置,则第一个if会引发错误;在未定义控制台的环境中访问控制台就像访问任何未定义的变量一样;它将引发ReferenceError.
尝试:
if( typeof console != "undefined" && console.log ) {
要么:
if(window.console && console.log) {
由于Firefox和Chrome和Safari一样实现了Firebug API,因此在Firefox中不会引发错误.但是,默认情况下,Internet Explorer不会,因此,这里值得进行适当的功能检查,因为它会在未实现此API的浏览器中引发ReferenceError.
标签:htmlunit,javascript,java,rhino 来源: https://codeday.me/bug/20191101/1986842.html