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();
}
}
Aggregations