问题
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.
回答1:
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?
回答2:
There is a JavaScript exception thrown there. That means there is something in that JS code that HTMLUnit (Rhino) doesn't like. You should firstly understand that not throwing an exception will not fix a bug in the JS code, if any. So if that issue is not allowing you to perform an action, hiding it or showing it, would still not allow you to perform the action.
Having said that, the setThrowExceptionOnScriptError
method determines if you want the getPage
to forward the unavoidable exception detected in the JS code to your application. In other words, if you want to throw the exception then you will most likely need a try-catch-finally block in your application wrapping the getPage
method.
Solution: Fix your JS code.
来源:https://stackoverflow.com/questions/23659340/option-setthrowexceptiononscripterrorfalse-not-work-in-htmlunit-why-java