Search in sources :

Example 1 with DomNode

use of com.gargoylesoftware.htmlunit.html.DomNode in project yyl_example by Relucent.

the class HtmlUnitTest3 method main.

public static void main(String[] args) throws Exception {
    WebClient webClient = new WebClient(BrowserVersion.FIREFOX_68);
    try {
        webClient.setCssErrorHandler(new SilentCssErrorHandler());
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        webClient.getOptions().setCssEnabled(true);
        webClient.getOptions().setRedirectEnabled(false);
        webClient.getOptions().setAppletEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setPopupBlockerEnabled(true);
        webClient.getOptions().setTimeout(10000);
        // JS运行错误时,是否抛出异常
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        // webClient.waitForBackgroundJavaScript(10 * 1000);
        HtmlPage page = webClient.getPage("https://www.baidu.com/");
        WebWindow webWindow = page.getEnclosingWindow();
        System.out.println("# 等待页面加载");
        waitFor(() -> {
            DomElement input = page.getElementById("kw");
            return input instanceof HtmlInput;
        });
        System.out.println("# 文本框输入 htmlunit ");
        HtmlInput kw = (HtmlInput) page.getElementById("kw");
        kw.setAttribute("value", "htmlunit");
        System.out.println("# 触发回车事件");
        Thread.sleep(1000);
        // Enter
        kw.type(13);
        System.out.println("# 等待页面跳转");
        Thread.sleep(1000);
        HtmlPage page2 = (HtmlPage) webWindow.getEnclosedPage();
        System.out.println(page2.getUrl());
        DomNodeList<DomNode> nodes = page2.querySelectorAll(".result.c-container h3 a");
        System.out.println("# 输出结果");
        for (DomNode node : nodes) {
            System.out.println(node.asText());
        }
    } finally {
        webClient.close();
    }
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) NicelyResynchronizingAjaxController(com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController) SilentCssErrorHandler(com.gargoylesoftware.htmlunit.SilentCssErrorHandler) WebClient(com.gargoylesoftware.htmlunit.WebClient) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) WebWindow(com.gargoylesoftware.htmlunit.WebWindow)

Aggregations

NicelyResynchronizingAjaxController (com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController)1 SilentCssErrorHandler (com.gargoylesoftware.htmlunit.SilentCssErrorHandler)1 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)1 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)1 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)1 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)1 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)1