Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
116 views
in Technique[技术] by (71.8m points)

java - Why doesn't setThrowExceptionOnScriptError(false) work?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...