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);
多用于模拟用户点击
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.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"); //通过内容
HtmlOption ho = hs.getOptionByText("Text"); //通过内容
HtmlOption ho = hs.getOption(index); //通过下标
hs.getOptions() ; //获得全部
hs.getOptionSize(); //获得size
//选中
ho.setSelected(true);
Java代码
// 或取一个的超链接
//HtmlAnchor anchor = (HtmlAnchor) page.getAnchorByName("welcome");
page = (HtmlPage) page.getAnchorByName("welcome").click();
String pageContent = page.getWebResponse().getContentAsString();
System.out.println(pageContent);
a. 获得table
1) 由tableID获得
HtmlTable table = (HtmlTable)page.getElementById("tableId");
2) 由tableName获得
DomNodeList
final HtmlTable table = (HtmlTable) tables2.get(tables2 .size() - 1);
b. 将table数据存放在map中,key值为1…n
Map
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++;
}
webClient.closeAllWindows();
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();