HtmlUnit忽略JavaScript错误?
我试图遍历一个网站,但在他们的一个页面上,我发现以下错误:HtmlUnit忽略JavaScript错误?,java,htmlunit,htmlunit-driver,Java,Htmlunit,Htmlunit Driver,我试图遍历一个网站,但在他们的一个页面上,我发现以下错误: 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 (
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)
我是否可以忽略这个错误?我并不特别关心日历是否正确加载。您可以使用以下类方法: 或者将问题区域封闭在try-catch块中-
try {
// code with error
}catch(e) {
// handle error
}
Alexey的答案是针对HttpUnit。对于HtmlUnit,代码类似
WebClient client = new WebClient();
client.getOptions().setThrowExceptionOnScriptError(false);
我尝试使用Matthews answer,并需要使用以下方法重写newWebClient()方法:
HtmlUnitDriver driver = new HtmlUnitDriver(true) {
@Override
protected WebClient newWebClient(BrowserVersion version) {
WebClient webClient = super.newWebClient(version);
webClient.getOptions().setThrowExceptionOnScriptError(false);
return webClient;
}
};
HttpUnit
如何引用HtmlUnit
?似乎这些是不同的框架…解决方案基于设置最终字段HtmlUnit如何引用WebClient?@user12384512-HtmlUnit有自己的WebClient类-com.gargoylesoftware.HtmlUnit.WebClient
;还要检查下面的@jseteny answer以了解其用法。
HtmlUnitDriver driver = new HtmlUnitDriver(true) {
@Override
protected WebClient newWebClient(BrowserVersion version) {
WebClient webClient = super.newWebClient(version);
webClient.getOptions().setThrowExceptionOnScriptError(false);
return webClient;
}
};