19

I'm trying to traverse through a website but on one of their pages I get this error:

EcmaError: lineNumber=[671] column=[0] lineSource=[null] name=[TypeError] sourceName=[https://reservations.besodelsolresort.com/asp/CalendarPopup.js] message=[TypeError: Cannot read property "parentNode" from undefined (https://reservations.besodelsolresort.com/asp/CalendarPopup.js#671)]
com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot read property "parentNode" from undefined (https://reservations.besodelsolresort.com/asp/CalendarPopup.js#671)

Is there anyway I can just ignore this error? I don't particularly care if the calendar loads properly.

cantread
  • 405
  • 1
  • 7
  • 14
  • solution based on setting final field http://stackoverflow.com/questions/8745061/is-it-possible-to-ignore-javascript-exceptions-when-working-with-webdriver-html/35037593#35037593 – user3093030 Jan 27 '16 at 13:15

3 Answers3

33

Alexey's answer is for HttpUnit. For HtmlUnit the code is similar

WebClient client = new WebClient();
client.getOptions().setThrowExceptionOnScriptError(false);
Matthew Molloy
  • 1,166
  • 1
  • 10
  • 22
10

I tried to use Matthews answer and needed to override the newWebClient() method using the following way:

    HtmlUnitDriver driver = new HtmlUnitDriver(true) {
        @Override
        protected WebClient newWebClient(BrowserVersion version) {
            WebClient webClient = super.newWebClient(version);
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            return webClient;
        }
    };
Erica Kane
  • 3,137
  • 26
  • 36
jseteny
  • 412
  • 1
  • 6
  • 10
-1

You can use the following methods of HttpUnitOptions class:

//change the scriptingEnabled flag
static void setScriptingEnabled(boolean scriptingEnabled)

// Determines whether script errors result in exceptions or warning messages.
static void setExceptionsThrownOnScriptError(boolean throwExceptions)

Or enclose the problem area in try-catch block -

try {
   // code with error

}catch(e) {
   // handle error
}