HtmlUnit简单用法

来源:互联网 发布:网络诈骗电话举报中心 编辑:程序博客网 时间:2024/06/05 03:21

Contents

1.作用 

2.获得数据 

3.模拟点击 

4.超链接 

5.关闭 

6.js支持 



注意:

webClient.setCssEnabled(false);  不使用css

webClient.setJavaScriptEnabled(false);  //不使用js

或者:

webClient.getOptions().setJavaScriptEnabled(true);  

//  webClient.getOptions().setActiveXNative(false);  

webClient.getOptions().setCssEnabled(false);  



1. 作用

多用于模拟用户点击

 

 

 

2. 获得数据

2.1.  获得网页数据,初始化

final WebClient  webClient = new WebClient();

HtmlPage page ;

/**

 * 初始化

 */

void init(){

String url1 = "http://127.0.0.1:8080/ActivityWeb/Web/User/MyJsp.jsp";

webClient.setCssEnabled(false);

webClient.setJavaScriptEnabled(false);

   

    try {

page=webClient.getPage(url1);

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

}

}

 

 

2.2. 输出htmlPage数据

/**

 * 输出所有内容

 * @param page

 */

void printAllText(HtmlPage page){

System.out.println(page.asText());

//webClient.closeAllWindows(); //未必要close

}

 

 

 

 

 

3.  模拟点击

3.1. 例子

/**

 * 模拟提交表单

 */

void actionTest(HtmlPage page){

//获取表单 

final HtmlForm form = page.getFormByName("f");

//获取提交按扭

final HtmlSubmitInput button = form.getInputByName("s1");

//一会得输入的

final HtmlTextInput textField = form.getInputByName("wd");

textField.setValueAttribute("魔兽世界");

//点击提交表单

try {

//[1]仅提交

//button.click();

//[2]提交后接收新页面

final HtmlPage page2 = button.click();

printAllText(page2);

catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

 

3.2. Input控件

(a) HtmlForm 表单:

//由表单获得控件

HtmlTextInput startTimeText = form.getInputByName("start_date");

(b) HtmlSubmitInput 提交控件

//点击并获得新页面

HtmlPage  page2 = button.click();

 

(c) HtmlTextInput 输入框

//修改value

inputText.setValueAttribute(Str);

 

 

(d) HtmlRadioButtonInput 单选框

//设置选中

radio.setChecked(true);

 

(e) HtmlImageInput  图片

//点击并获得新页面

HtmlPage  page2 = (HtmlPage)Image.click();

 

 

(f) HtmlSelect 下拉选择   HtmlOption选择其中的一项

//获得下拉单 page中通过id获得

HtmlSelect hs = (HtmlSelect) page.getElementById("ddlVesselName");

//获得下拉单中的一项

HtmlOption ho= hs.getOptionByValue("CSCOK");  //通过value

HtmlOption ho = hs.getOptionByText("Text");   //通过内容<option>text</option >

HtmlOption ho = hs.getOptionByText("Text");   //通过内容<option>text</option >

HtmlOption ho = hs.getOption(index);      //通过下标 

hs.getOptions() ;        //获得全部

hs.getOptionSize();         //获得size

 

//选中

ho.setSelected(true);

 

 

 

 

 

4. 超链接

Java代码 

// 或取一个<a>的超链接   

//HtmlAnchor anchor = (HtmlAnchor) page.getAnchorByName("welcome");   

        page = (HtmlPage) page.getAnchorByName("welcome").click();   

        String pageContent = page.getWebResponse().getContentAsString();   

        System.out.println(pageContent);  

 

 

 

5. 处理table

a. 获得table

1) 由tableID获得

HtmlTable  table = (HtmlTable)page.getElementById("tableId");

2) 由tableName获得

DomNodeList<HtmlElement> tables2 = ahtmpr .getElementsByTagName("tableName"); 
final HtmlTable table = (HtmlTable) tables2.get(tables2 .size() - 1); 

 

 

 

b. 将table数据存放在map中,key值为1…n

Map<String, String[]> map = new HashMap<String, String[]>(); 

int key = 1; 

for (final HtmlTableRow row : table.getBodies().get(0).getRows()) { 

String[] alertPring = new String[7]; 

int d = 0; 

for (final HtmlTableCell cell : row.getCells()) { 

alertPring[d] = cell.asText(); 

d++; 

    map.put(key +"", alertPring); 

    key++; 

}

 

 

 

 

 

6. 关闭

webClient.closeAllWindows();

 

 

 

7. js支持

a. 获得js方法名

HtmlInput input = form.getInputByName("ctl00$ASPxSplitter2$ContentPlaceHolder1$SearchPages$cbVessel");

String functionName_onKeyDown = input.getOnKeyDownAttribute();  //获得onkekDown方法名

 

b. 执行js方法

ScriptResult exportResult = page.executeJavaScript(“javascript:wrapperSetCube(‘/ddp’)”);

page = (HtmlPage) scriptPage.getNewPage();

0 0
原创粉丝点击