I am using JDK+NetBeans. I downloaded HtmlUnit, and tried all versions between 2.9 and 2.14, and none worked with this function. For 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();
}
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=[<no source>] 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 executed. But why, when I used the webClient.getOptions().setThrowExceptionOnScriptError(false);
option? And exception on function call VKVolumeDown()
must be ignored?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…