问题描述
我在主题中的问题。 /我使用JDK + NetBeans /。所以,我从 2.9-2.14之间的任何版本,没有人不能使用此功能。例如我的代码(java):
.....
import com.gargoylesoftware.htmlunit.AlertHandler;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.ScriptPreProcessor;
import com.gargoylesoftware.htmlunit.ScriptResult;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine;
.....
.....
public static void main(String [] args)throws异常{
WebClient webClient = new WebClient(BrowserVersion .FIREFOX_24);
webClient.getOptions()。setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions()。setThrowExceptionOnScriptError(false);
webClient.getOptions()。setPrintContentOnFailingStatusCode(false);
HtmlPage page = webClient.getPage(file:/// D:/WebRoot/tmp2/test.html);
webClient.closeAllWindows();
}
和test.html:
< html>
< head>
< / head>
< body>
< script>
VKVolumeDown();
alert(Hello);
< / script>
< / body>
< / html>
...我得到脚本例外:
INFO:抓取脚本异常
======= EXCEPTION START ========
EcmaError:lineNumber = [82] column = [0] lineSource = [] name = [ReferenceError] sourceName = [脚本文件:/ D:/WebRoot/tmp2/test.html从(7,11)到(24,12)] message = [ReferenceError:VKVolumeDown未定义。 (文件中的脚本:/ D:/WebRoot/tmp2/test.html从(7,11)到(24,12)#82)]
com.gargoylesoftware.htmlunit.ScriptException:ReferenceError:VKVolumeDown是没有定义的。 (文件中的脚本/ / D:/WebRoot/tmp2/test.html从(7,11)到(24,12)#82)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine $ HtmlUnitContextAction.run JavaScriptEngine.java:689)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:620)
.... 。
和alert(Hello);不是执行:(但是为什么我使用webClient.getOptions()。setThrowExceptionOnScriptError(false);选项???异常函数调用VKVolumeDown()必须被忽略!谢谢你,谢谢你快速回复,Mosty Mostacho。据我所知:模拟在JavaScript代码中尝试{} catch(err){}在HtmlUnit工具中不会发布,我的GENERAL任务正在遍历所有的JavaScript代码(从头到尾),如果可能,执行所有的结构。在try {} catch(err){}块中包装每个JavaScript构造,但它不是很优雅,也许谁提出任何变体?
My problem in a topic. /I am use JDK+NetBeans/. So, I download HtmlUnit from http://sourceforge.net/projects/htmlunit/files/htmlunit/ any version between 2.9 -2.14 and no one not work with this function. Fore example my code (java):
.....
import com.gargoylesoftware.htmlunit.AlertHandler;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.ScriptPreProcessor;
import com.gargoylesoftware.htmlunit.ScriptResult;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine;
.....
.....
public static void main(String[] args) throws Exception {
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setPrintContentOnFailingStatusCode(false);
HtmlPage page = webClient.getPage("file:///D:/WebRoot/tmp2/test.html");
webClient.closeAllWindows();
}
and test.html:
<html>
<head>
</head>
<body>
<script>
VKVolumeDown();
alert("Hello");
</script>
</body>
</html>
...and I get script exception:
INFO: Caught script exception======= EXCEPTION START ========EcmaError: lineNumber=[82] column=[0] lineSource=[] name=[ReferenceError] sourceName=[script in file:/D:/WebRoot/tmp2/test.html from (7, 11) to (24, 12)] message=[ReferenceError: "VKVolumeDown" is not defined. (script in file:/D:/WebRoot/tmp2/test.html from (7, 11) to (24, 12)#82)]com.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "VKVolumeDown" is not defined. (script in file:/D:/WebRoot/tmp2/test.html from (7, 11) to (24, 12)#82) at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:689) at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:620)
.....
And "alert("Hello");" is not execute:(. But why if I use "webClient.getOptions().setThrowExceptionOnScriptError(false);" option??? And exception on function call "VKVolumeDown()" must be ignored!? It confuse me very strong. Help me please, it is very important for me. Thanks.
Thanks for quick response, Mosty Mostacho. As I understand: analog of construction "try{}catch(err){}" in JavaScript code is not release in HtmlUnit tool. My GENERAL task is walking through all JavaScript code (from begin to end) and execute all constructions if it possible.I see variant about wrapped each JavaScript construction in try{}catch(err){} block, but it is not so elegant. Maybe who propose any variants?
这篇关于选项“setThrowExceptionOnScriptError(false)”不工作在HtmlUnit!为什么? (JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!