1

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

  • Is there a specific line that the exception is propagating from? – Reilas May 27 '23 at 16:10
  • @Reilas Hi, I have added more details to the question, from the exception that I'm getting. I think it is happening when driver clicks on the submit button and next page starts loading. I'm just trying to find if I can set some config or whatever not to fail because of that kind of errors, if that's possible. Thanks – Aleksandar Grujic May 27 '23 at 16:53
  • You're _try-catch_ is missing a bracket. – Reilas May 27 '23 at 18:43

2 Answers2

1
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.

RBRi
  • 2,704
  • 2
  • 11
  • 14
  • Ah, you mean that this what I am seeing is not the error that is thrown, it is just a log? I thought it is actually an error being thrown. – Aleksandar Grujic May 27 '23 at 17:59
  • yes, place some system.out code in your code (at least after getPage() line and you will see that you are passing this. – RBRi May 28 '23 at 07:08
  • btw i can't reach the page here to reproduce your problem - is the url correct? – RBRi May 28 '23 at 07:16
  • Yes, you are right! It is actually just a log. Silly me. For the url, I needed to post same fake one, couldn't shared the right one - privacy concerns. Thanks for helping out. – Aleksandar Grujic May 28 '23 at 10:12
  • However, now I'm facing different issue - after the login, I'm seeing this in the body of the new page: BODY: Sadly, your browser is not supported. SCP works best on: Google Chrome 45+, Mozilla Firefox 45+, Apple Safari 10+, and Microsoft Edge 13+Other legacy browsers (like Microsoft Internet Explorer 11) are no longer supported. Should I post a separate question? – Aleksandar Grujic May 28 '23 at 10:33
  • Please open an issue at github and send me the url via private mail - will have a look. – RBRi May 28 '23 at 14:22
  • Done. Just sent you the link. – Aleksandar Grujic May 28 '23 at 15:15
0

I used JSLint.com for this.

You are missing a closing block bracket, }, for the try portion of your try-catch block.

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

Should be

} catch (Exception e){
    System.out.println("ERROR " + e);
}
Reilas
  • 3,297
  • 2
  • 4
  • 17
  • Hi @Reilas, sorry, the closing block bracket is missing only in the code snippet. Apologies, I accidentally removed it while I was doing a copy/paste. But it is there, in the actual code. My question was for the error that is coming from the javascript files, I was wondering why I would get that error, since it is not in my code. But it turned out that it is just a log that htmlUnit is generating. And there are really a lot of logs that are making noise. – Aleksandar Grujic May 27 '23 at 20:02