htmlunit 无法从未定义中读取 属性 "push"

htmlunit Cannot read property "push" from undefined

我正在尝试使用 htmlunit 抓取网站。每当我 运行 它只输出以下错误时:

Caused by: net.sourceforge.htmlunit.corejs.javascript.EcmaError: TypeError: Cannot read property "push" from undefined (https://www.kinoheld.de/dist/prod/0.4.7/widget.js#1)

现在我对JS了解不多,但是看过push是某种数组操作。这对我来说似乎是标准的,我不知道为什么 htmlunit 不支持它。

这是我目前使用的代码:

public static void main(String[] args) throws IOException {
    WebClient web = new WebClient(BrowserVersion.FIREFOX_45);
    web.getOptions().setUseInsecureSSL(true);
    String url = "https://www.kinoheld.de/kino-muenchen/royal-filmpalast/vorstellung/280823/?mode=widget&showID=280828#panel-seats";
    web.getOptions().setThrowExceptionOnFailingStatusCode(false);
    web.waitForBackgroundJavaScript(9000);
    HtmlPage response = web.getPage(url);

    System.out.println(response.getTitleText());
}

我错过了什么?有没有办法解决这个问题或解决这个问题的方法? 提前致谢!

尝试添加

web.getOptions().setThrowExceptionOnScriptError(false);

在您尝试获取页面之前。这会强制 htmlunit 忽略错误。但是,如果抛出错误的 javascript 对于获取您正在废弃的数据很重要(希望它不是),那么这可能不会在 100% 的时间内起作用。如果这不起作用,请尝试将 Selenium 与 ChromeDriver 或 GhostDriver 结合使用。

Source

我以前遇到过类似的问题。这是 HTML 单元被设计为测试工具框架而不是网络抓取框架的问题。您 运行 是 HTML Unit 的最新版本吗?

我能够 运行 通过添加 setThrowExceptionOnScriptError(false) (如 Coffee Converter 的回答中所述)行以及添加 java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF); 在禁用日志转储的方法顶部。这产生了输出:

Royal Filmpalast München München | kinoheld.de

完整代码如下:

public static void main(String[] args) throws IOException {

    java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);

    WebClient webClient = new WebClient(BrowserVersion.FIREFOX_45);
    String url = "https://www.kinoheld.de/kino-muenchen/royal-filmpalast/vorstellung/280823/?mode=widget&showID=280828#panel-seats";

    webClient.getOptions().setUseInsecureSSL(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.waitForBackgroundJavaScript(9000);
    HtmlPage response = webClient.getPage(url);

    System.out.println(response.getTitleText());
}

这是 运行 RedHat 命令行上的 HTML 单元 2.2.1。希望这有帮助。