javahtmlunithtmlunit-driver

How to prevent HtmlUnit from throwing an error that is in the javascript?


I'm trying to login to one website, and after entering successfully username and password, and after clicking on submit button, I'm getting the following:

2023-05-27 16:46:41.864 ERROR 296 --- [nio-8080-exec-2] c.g.h.j.DefaultJavaScriptErrorListener   : Error during JavaScript execution

com.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "scp" is not defined. (https://me.scp.com/resources/~202305241435~/scp-ui-custom-3.js#17293)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:1001) ~[htmlunit-2.70.0.jar:2.70.0]
        at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:590) ~[htmlunit-core-js-2.70.0.jar:na]
...
caused by: 2023-05-27 16:46:40.968 ERROR 296 --- [nio-8080-exec-2] c.g.h.j.DefaultJavaScriptErrorListener   : Error during JavaScript execution
com.gargoylesoftware.htmlunit.ScriptException: missing ; before statement (https://me.ssp.com/resources/~202305241435~/scp-ui-custom-2.js#58)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:1001) ~[htmlunit-2.70.0.jar:2.70.0]
        at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:590) ~[htmlunit-core-js-2.70.0.jar:na] 

I don't care if there is some error in some Javascript code on that site, I just want to get some element when the page loads. And the page is loading just fine, when I go to it manually.

Here is my code, and what I have tried:

     try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX)) {
        webClient.getOptions().setThrowExceptionOnScriptError(false);

        HtmlPage page = webClient.getPage("https://me.spc.com/systems");

        HtmlForm loginForm = (HtmlForm) page.getElementById("logOnForm");

        loginForm.getInputByName("j_username").type("my_username");

        HtmlElement userNameSubmit = (HtmlElement) page.getElementById("logOnFormSubmit");
        page = (HtmlPage) userNameSubmit.click();

        HtmlForm passForm = (HtmlForm) page.getElementById("logOnForm");

        passForm.getInputByName("j_password").type("password123");

        HtmlElement passwordSubmit = (HtmlElement) page.getElementById("logOnFormSubmit");
        
        page = (HtmlPage) passwordSubmit.click();

    catch (Exception e){
        System.out.println("ERROR " + e);
    }

As you can see, I've added the webClient.getOptions().setThrowExceptionOnScriptError(false), but it didn't help.

Thanks


Solution

  • webClient.getOptions().setThrowExceptionOnScriptError(false);
    

    prevents throwing the exception but still logs the exception.

    So you did everything right - just ignore the log output (if you are sure that this js is not used) or disable the logging.

    Technical details: The exception is triggered by the js engine, catched by HtmlUnit, always logged (like the browsers doing in the console) and if ThrowExceptionOnScriptError is true the exception is thrown - otherwise ignored.