HtmlUnit ignore JavaScript errors?

2022-09-02 11:53:01

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

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)

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


答案 1

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

WebClient client = new WebClient();
client.getOptions().setThrowExceptionOnScriptError(false);

答案 2

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

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

推荐