15

Is it possible to teach HTMLUnit to ignore certain javascript scripts/files on a web page? Some of them are just out of my control (like jQuery) and I can't do anything with them. Warnings are annoying, for example:

[WARN] com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument:
getElementById(script1299254732492) did a getElementByName for Internet Explorer

Actually I'm using JSFUnit and HTMLUnit works under it.

1
  • 1
    The HTMLUnit mailing list would be a good place to ask. The developers are on it, and they're generally very helpful. Commented Mar 9, 2011 at 0:06

5 Answers 5

4

If you want to avoid exceptions because of any JavaScript errors:

 webClient.setThrowExceptionOnScriptError(false);        
Sign up to request clarification or add additional context in comments.

1 Comment

Or for 2.13 and above: webClient.getOptions().setThrowExceptionOnScriptError(false);
1

Well I am yet to find a way for that but I have found an effective workaround. Try implementing FalsifyingWebConnection. Look at the example code below.

public class PinConnectionWrapper extends FalsifyingWebConnection {

    public PinConnectionWrapper(WebClient webClient)
            throws IllegalArgumentException {
        super(webClient);
    }

    @Override
    public WebResponse getResponse(WebRequest request) throws IOException {
        WebResponse res = super.getResponse(request);
        if(res.getWebRequest().getUrl().toString().endsWith("/toolbar.js")) {
            return createWebResponse(res.getWebRequest(), "",
"application/javascript", 200, "Ok");
        }
        return res;
    }

}

In the above code whenever HtmlUnit will request for toolbar.js my code will simply return a fake empty response. You can plug-in your above wrapper class into HtmlUnit as below.

final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
new PinConnectionWrapper(webClient);

Comments

0

Take a look at WebClient.setScriptPreProcessor. It will give you the opportunity to modify (or in your case, stop) a given script before it is executed.

Also, if it is just the warnings getting on your nerves I would suggest changing the log level.

Comments

0

If you are interested in ignoring all warning log entries you can set the log level to INFO for com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument in the log4j.properties file.

Comments

0

LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF); java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);

Insert this code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.