HTMLUnit无法找到元素

问题描述

我正在使用HtmlUnit监视网页,但似乎无法获取主要内容中的元素。我怀疑是因为该页面是使用Vue.js呈现的。

这是我正在监视的页面,我想在

获取内容

webpage HTML

这是我使用page.asXml()打印页面时的输出

为空。

HtmlUnit page.asXml()

这是我正在使用的WebClient代码,已经启用了JavaScript。

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

WebClient webClient = new WebClient();
webClient.getoptions().setUseInsecureSSL(true);
webClient.getoptions().setJavaScriptEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        
webClient.getoptions().setThrowExceptionOnScriptError(false);
webClient.getoptions().setThrowExceptionOnFailingStatusCode(false);
webClient.setJavaScriptErrorListener(new SilenceJavaScriptErrorListner());
webClient.setCssErrorHandler(new SilentCssErrorHandler());

这是函数内部的代码,在返回之前,我要等待

中的某个元素存在。我也使用过waitForBackgroundJavaScript()方法
HtmlPage page = (HtmlPage) webClient.getCurrentwindow().getEnclosedPage();
webClient.waitForBackgroundJavaScript(10000);

for (int i = 0; i < 10; i++) {
    page = (HtmlPage) webClient.getCurrentwindow().getEnclosedPage();
    webClient.waitForBackgroundJavaScript(10000);
    log.info("Current page \n" + page.asXml());
                
    List<Object> quoteNumberOptionList = page.getByXPath("someXPath");
                
    if (quoteNumberOptionList.size() > 0) {
        break;
    }
                
    Thread.sleep(5000);
}

1 个答案:

答案 0 :(得分:0)

由于您在上面的评论中提到您无法共享URL(而且无论如何它都可能无法公开访问),因此我在这里做了一些撰写,可以为您提供帮助Parsing web javascript content to string using android

解决方法

由于您在上面的评论中提到您无法共享URL(而且无论如何它都可能无法公开访问),因此我在这里做了一些撰写,可以为您提供帮助Parsing web javascript content to string using android