HtmlUnit忽略JavaScript错误?
我正试图遍历一个网站,但在他们的一个页面上我收到此错误:
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)
无论如何我可以忽略这个错误吗? 如果日历加载正确,我并不特别在意。
Alexey的答案是HttpUnit。 对于HtmlUnit,代码类似
WebClient client = new WebClient(); client.getOptions().setThrowExceptionOnScriptError(false);
我尝试使用Matthews的答案,并需要使用以下方式覆盖newWebClient()方法:
HtmlUnitDriver driver = new HtmlUnitDriver(true) { @Override protected WebClient newWebClient(BrowserVersion version) { WebClient webClient = super.newWebClient(version); webClient.getOptions().setThrowExceptionOnScriptError(false); return webClient; } };
您可以使用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 }