Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...