如何使用htmlunit自动填充表单?

时间:2015-01-13 05:12:10

标签: jsoup htmlunit

我想在输入网站的网址时自动填写并自动提交网站中的所有文本框。任何人都可以帮我提供jsoup或htmlunit或java中的代码吗?

1 个答案:

答案 0 :(得分:0)

以下是HTMLUnit

的示例
public void submittingForm() throws Exception {
    final WebClient webClient = new WebClient();

    // Get the first page
    HtmlPage page1 = webClient.getPage("http://some_url");

    // Get the form that we are dealing with and within that form, 
    // find the submit button and the field that we want to change.
    HtmlForm form = page1.getFormByName("myform");

    HtmlSubmitInput button = form.getInputByName("submitbutton");
    HtmlTextInput textField = form.getInputByName("userid");

    // Change the value of the text field
    textField.setValueAttribute("root");

    // Now submit the form by clicking the button and get back the second page.
    HtmlPage page2 = button.click();

    webClient.closeAllWindows();
}