19

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

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)

无论如何我可以忽略这个错误吗?我并不特别关心日历是否正确加载。

4

3 回答 3

33

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

WebClient client = new WebClient();
client.getOptions().setThrowExceptionOnScriptError(false);
于 2014-08-31T11:31:38.833 回答
10

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

    HtmlUnitDriver driver = new HtmlUnitDriver(true) {
        @Override
        protected WebClient newWebClient(BrowserVersion version) {
            WebClient webClient = super.newWebClient(version);
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            return webClient;
        }
    };
于 2015-12-10T10:41:03.347 回答
0

您可以使用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)

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

try {
   // code with error

}catch(e) {
   // handle error
}
于 2014-01-22T22:35:46.217 回答