【问题标题】:HtmlUnit ignore JavaScript errors?HtmlUnit 忽略 JavaScript 错误?
【发布时间】:2014-02-13 04:56:54
【问题描述】:

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

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)

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

【问题讨论】:

标签: java htmlunit htmlunit-driver


【解决方案1】:

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

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

【讨论】:

  • HtmlUnit 是如何引用 WebClient 的?
  • @user12384512 - HtmlUnit 有自己的 WebClient 类 - com.gargoylesoftware.htmlunit.WebClient;还要检查下面的@jseteny 答案以了解其用法。
【解决方案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;
        }
    };

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • HttpUnit 如何引用HtmlUnit?似乎这些是不同的框架......
    猜你喜欢
    • 2020-09-29
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 2020-04-01
    相关资源
    最近更新 更多