package com.lxl.htmlutil.test;
import java.io.IOException;
import org.junit.Test;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
public class HtmlUtilTest {
@Test
public void test() throws IOException {
// 创建浏览器,可以选择IE、FF等等
WebClient client = new WebClient();
//htmlunit 对css和javascript的支持不好,所以请关闭之
client.getOptions().setJavaScriptEnabled(false);
client.getOptions().setCssEnabled(false);
// 获取某网站页面
HtmlPage loginPage = client
.getPage("http://175.43.121.77/mblog/login");
HtmlElement passwordElmt = loginPage.getElementByName("password");
passwordElmt.click();
passwordElmt.type("3323356");
// 获取某页面元素,可通过id或name,(具体方式很多 --Foxswily)
HtmlElement usernameElmt = loginPage.getElementByName("username");
// HtmlElement elmt = page.getElementByName("somename");
// 此例以文本框为例,先点击,再输入,完全跟实际浏览器行为一致
usernameElmt.click();
usernameElmt.type("q260996583");
// 获取按钮
DomNodeList<DomElement> loginBtn = loginPage.getElementsByTagName("input");
// HtmlSubmitInput loginBtn = (HtmlSubmitInput) loginPage
// .getElementById("LoginAction_login");
// HtmlButton loginBtn = (HtmlButton)
// page.getElementById("LoginAction_login");
HtmlSubmitInput input = null;
for(DomElement dom : loginBtn) {
if(dom.getAttribute("type").equals("submit")){
input = (HtmlSubmitInput) dom;
}
}
// 点击并获得返回结果
HtmlPage loginResultPage = input.click();
// 结果拿到了,想干啥您随意
String loginResultContent = loginResultPage.getWebResponse()
.getContentAsString();
System.out.println(loginResultContent);
// 如果登录成功
if (null != loginResultContent
&& loginResultContent.indexOf("龙马") > 0) {
// 获得HTML链接并点击该链接
HtmlAnchor showbookBtn = (HtmlAnchor) loginResultPage
.getElementById("showbook");
HtmlPage showBookPage = showbookBtn.click();
System.out
.println("\n\n===========================================================");
System.out.println(showBookPage.getWebResponse()
.getContentAsString());
}
}
}
pom.xml
htmlunit 在httpclient项目上增加新功能,htmlunit中包含了httpclient、httpmime等jar包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lxl.test</groupId> <artifactId>SpringSplit</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringSplit Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- https://mvnrepository.com/artifact/net.sourceforge.htmlunit/htmlunit --> <dependency> <groupId>net.sourceforge.htmlunit</groupId> <artifactId>htmlunit</artifactId> <version>2.15</version> </dependency> </dependencies> <build> <finalName>htmlutil</finalName> </build> </project>