【问题标题】:Htmlunit can't load all elements of site, because of js exception由于js异常,Htmlunit无法加载站点的所有元素
【发布时间】:2018-09-05 04:42:58
【问题描述】:

我正在使用 htmlunit 库创建一些应用程序,但问题是由于 javascript 异常,一些非常重要的站点无法加载:

net.sourceforge.htmlunit.corejs.javascript.JavaScriptException: Error: fetch is not found globally and no fetcher passed, to fix pass a fetch for your environment like https://www.npmjs.com/package/unfetch.

我尝试使用client.getOptions().setThrowExceptionOnScriptError(false);,但它不起作用。如何解决?

【问题讨论】:

  • P.S.我无法更改网站本身的代码
  • 我已经看过了,但他使用的是javascript,我正在用htmlunit库编写java,你知道如何使用java实现这个解决方案吗?

标签: java exception htmlunit


【解决方案1】:

从 HtlUnit 2.30 版开始,不支持/实现 Fetch API (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)。请为 HtmlUnit 打开一个问题(如果您愿意,请提供补丁)。

因为 Fetch API 在 IE 中不可用(据我所知)也许它是使用 HtmlUnit 的 IE 模拟的一个选项。

确定网站使用了 api 的哪一部分也很有帮助。然后我们可以先专注于实现这些东西。

【讨论】:

    【解决方案2】:

    您可以试试 Headless Chrome,它支持 fetch API。

    在此处下载 Chrome 驱动程序:https://chromedriver.storage.googleapis.com/index.html?path=2.38/

    或者在 macOS 上:

    brew install chromedriver
    

    然后在你的 pom.xml/graddle 中添加一个最新版本的 Selenium:

    <dependency>  
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.8.1</version>
    </dependency> 
    

    当然,您需要 Chrome 版本 > 59

    Java 部分来了:

    String chromeDriverPath = "/Path/To/Chromedriver" ;  
    System.setProperty("webdriver.chrome.driver", chromeDriverPath);  
    ChromeOptions options = new ChromeOptions();  
    options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors");  
    WebDriver driver = new ChromeDriver(options); 
    

    我在这里写了一篇详细说明的博客文章: https://ksah.in/introduction-to-chrome-headless/

    【讨论】:

      猜你喜欢
      • 2014-01-20
      • 2011-10-16
      • 2017-01-30
      • 2020-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多