我正在尝试通过网站穿越,但是在他们的一个页面上,我会遇到此错误:
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 class> class:
//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
}