Form提交
对于WEB应用,有着大量的表单,所以HtmlUnit提供了HtmlForm这个对象,以及表单操作的相应方法:
$cor = new HtmlUnitCollector(60, BrowserVersion.FIREFOX_3_6);
$cor.collect(new Url($url.toString() + "zhusu/"));
var form = $cor.getPage().getHtmlElementById("J-booking-hotel");
var ok = form.getInputByValue("搜索");
ok.click();
$doc = $cor.createDocument();
print($doc);
或
final WebClient webClient = new WebClient();
// 获取首页
final HtmlPage page1 = (HtmlPage) webClient.getPage("http://htmlunit.sourceforge.net");
// 根据form的名字获取页面表单,也可以通过索引来获取:page.getForms().get(0)
final HtmlForm form = page1.getFormByName("myform");
final HtmlSubmitInput button
= (HtmlSubmitInput) form.getInputByName("submitbutton");
final HtmlTextInput textField
= (HtmlTextInput) form.getInputByName("userid");
// 设置表单域的值
textField.setValueAttribute("root");
// 提交表单,返回提交表单后跳转的页面
final HtmlPage page2 = (HtmlPage) button.click();