HtmlUnit忽略JavaScript错误?

can*_*ead 17 java htmlunit htmlunit-driver

我正试图遍历一个网站,但在他们的一个页面上我收到此错误:

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)
Run Code Online (Sandbox Code Playgroud)

无论如何我可以忽略这个错误吗?如果日历加载正确,我并不特别在意.

Mat*_*loy 28

Alexey的答案是HttpUnit.对于HtmlUnit,代码类似

WebClient client = new WebClient();
client.getOptions().setThrowExceptionOnScriptError(false);
Run Code Online (Sandbox Code Playgroud)


小智 8

我尝试使用Matthews的答案,并需要使用以下方式覆盖newWebClient()方法:

    HtmlUnitDriver driver = new HtmlUnitDriver(true) {
        @Override
        protected WebClient newWebClient(BrowserVersion version) {
            WebClient webClient = super.newWebClient(version);
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            return webClient;
        }
    };
Run Code Online (Sandbox Code Playgroud)


小智 -1

您可以使用HttpUnitOptions类的以下方法:

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

// Determines whether script errors result in exceptions or warning messages.
static void setExceptionsThrownOnScriptError(boolean throwExceptions)
Run Code Online (Sandbox Code Playgroud)

或者将问题区域包含在 try-catch 块中 -

try {
   // code with error

}catch(e) {
   // handle error
}
Run Code Online (Sandbox Code Playgroud)

  • “HttpUnit”如何引用“HtmlUnit”?似乎这些是不同的框架...... (8认同)