【问题标题】:How to get html which dom generated after the Javascricpt of view source in java? [duplicate]java - 如何获取java中查看源代码的Javascricpt后生成的html? [复制]
【发布时间】:2017-12-17 17:00:06
【问题描述】:

我曾尝试过 Htmlunit,但它不起作用,所以你可以帮助我使用 API 或任何其他我可以研究并获得解决方案的解决方案。谢谢

import com.gargoylesoftware.htmlunit.BrowserVersion;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class GetPageSourceAfterJS {
    public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {

        java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF); /* comment out to turn off annoying htmlunit warnings */
        WebClient webClient;
        webClient = new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setJavaScriptEnabled(true);
        //System.out.println("Loading page now: "+url);
        HtmlPage page = webClient.getPage("http://www.thehindu.com/");
        webClient.waitForBackgroundJavaScript(30 * 1000); /* will wait JavaScript to execute up to 30s */

        String pageContent = page.asText();
        System.out.println("pageContent");

        //get divs which have a 'class' attribute of 'bucket'
       // List<?> buckets = page.getByXPath("//div[@class='bucket']");
       // System.out.println("Found "+buckets.size()+" 'bucket' divs.");

        //System.out.println("#FULL source after JavaScript execution:\n "+pageAsXml);
    }
} can u tell me why this failing to run

【问题讨论】:

  • 你在问什么?这和java有什么关系?到目前为止你尝试过什么?
  • 在执行页面的 javascript 后保存页面的 HTML 输出我想知道这个我看过 selenium web 驱动程序,但它已经进入测试部分,所以我不清楚,我使用过 Htmlunit

标签: javascript java dom htmlunit htmlunit-driver


【解决方案1】:

如果DOM生成的元素有ID,可以通过

<someVariable> = document.getElementById(id);

然后得到&lt;someVariable&gt;.outerHTML

【讨论】:

  • thnx 但我解决了问题
猜你喜欢
  • 1970-01-01
  • 2011-08-23
  • 2010-12-05
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2015-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多