本文介绍了htmlunit 模拟登入、点击、获取页面信息的demo
public static String getHtml(String url) {
System.out.println("****************开始执行****************");
// 模拟一个浏览器
@SuppressWarnings("resource")
WebClient webClient = new WebClient();
// webclient参数载体
WebClientOptions clientOptions = webClient.getOptions();
// 设置webClient的相关参数
clientOptions.setJavaScriptEnabled(true);
clientOptions.setCssEnabled(false);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getCookieManager().setCookiesEnabled(true);
webClient.getOptions().setRedirectEnabled(true);
clientOptions.setTimeout(35000);
clientOptions.setThrowExceptionOnScriptError(false);
// 模拟浏览器打开一个目标网址
HtmlPage rootPage = null;
try {
rootPage = webClient.getPage(url);
// 等待JS驱动dom完成获得还原后的网页
webClient.waitForBackgroundJavaScript(10000);
System.out.println(new Date() + "--开始登入tplink");
HtmlPasswordInput input = (HtmlPasswordInput)rootPage.getElementById("lgPwd");
//设置密码
input.setValue("设置你的密码");
HtmlButtonInput select = (HtmlButtonInput)rootPage.getElementById("loginSub");
HtmlPage page = select.click();
System.out.println(new Date() + "--登入tplink");
webClient.waitForBackgroundJavaScript(10000);
System.out.println(new Date() + "--开始进入应用管理");
HtmlElement appsMgbtn = (HtmlElement)page.getElementById("appsMgtMbtn");
HtmlPage appsPage = appsMgbtn.click();
System.out.println(new Date() + "--进入应管理");
webClient.waitForBackgroundJavaScript(10000);
System.out.println(new Date() + "--开始进入ddns");
Iterable<DomElement> iterable = appsPage.getElementById("appsInstallList").getChildElements();
while (iterable.iterator().hasNext()) {
DomElement item = iterable.iterator().next();
DomNodeList<HtmlElement> list = item.getElementsByTagName("span");
if (list != null && list.size() > 0) {
String name = list.get(0).getVisibleText();
if (StringUtils.equals(name, "DDNS")) {
DomNodeList<HtmlElement> inputList = item.getElementsByTagName("input");
if (inputList.size() > 1) {
HtmlButtonInput target = (HtmlButtonInput)inputList.get(1);
HtmlPage pageDns = target.click();
System.out.println(new Date() + "--进入ddns");
webClient.waitForBackgroundJavaScript(10000);
HtmlButtonInput ddnsLogin = (HtmlButtonInput)pageDns.getElementById("ddnsLogin");
String display = ddnsLogin.getStyleElement("display").getValue();
if (!StringUtils.equals("none", display)) {
ddnsLogin.click();
System.out.println(new Date() + "--点击登入");
webClient.waitForBackgroundJavaScript(10000);
} else {
System.out.println(new Date() + "--已经登入,不用点击!");
}
}
}
}
}
} catch (FailingHttpStatusCodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// body html信息
HtmlElement htmlElement = rootPage.getBody();
String xmlContent = htmlElement.asXml();
webClient.close();
System.out.println("****************执行结束****************");
return xmlContent;
}
标签:htmlunit,--,System,登入,println,new,out,webClient,页面
From: https://www.cnblogs.com/lixiuming521125/p/17745056.html