HtmlUnit无法呈现Google地图

时间:2018-03-19 16:18:11

标签: java google-maps htmlunit

以下代码无法呈现Google地图:

String completeUrl = "https://www.google.com.ph/maps/search/wallmart";
System.out.println(completeUrl);
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52);
webClient.waitForBackgroundJavaScript(20000);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setRedirectEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getCookieManager().setCookiesEnabled(true);
webClient.setAjaxController(new AjaxController(){
    @Override
    public boolean processSynchron(HtmlPage page, WebRequest request, boolean async) {
        return true;
    }
});
//webClient.getOptions().setTimeout(20000);
HtmlPage page = webClient.getPage(completeUrl);

我们得到的是带有此代码片段的HTML:

<a class="no-script-help-link" href="//support.google.com/maps/?hl=en&amp;authuser=0&amp;p=no_javascript" target="_blank"> Enable JavaScript to see Google Maps. </a> 

此代码有什么问题?因为 20秒已经等待Javascript了。

1 个答案:

答案 0 :(得分:1)

很多事情:

  1. 使用最新的快照构建
  2. 从默认配置开始;如果你没有特别用于
  3. ,配置很多选项是没有意义的
  4. setAjaxController - 如果不需要,请不要使用
  5. 正如之前多次报道的那样:waitForBackgroundJavaScript不是一个选项,你必须在之后调用它,例如getPage或单击
  6. 已完成本地测试并在日志中遇到一些js错误。如果您希望修复此错误,请尝试隔离最小情况(http://htmlunit.sourceforge.net/submittingJSBugs.html)并向HtmlUnit错误跟踪器报告。