有一个网页,我想从中检索某个字符串。为此,我需要登录,单击一些按钮,填充文本框,单击另一个按钮 - 然后就会出现字符串。
我怎样才能编写一个java程序来自动执行此操作?是否有任何有用的库用于此目的?
Thanks
Try HtmlUnit http://htmlunit.sourceforge.net/
HtmlUnit 是一个“无 GUI 浏览器”
Java 程序”。它对 HTML 进行建模
文档并提供 API
允许您调用页面,填写
表格、点击链接等...就像
您可以在“普通”浏览器中执行此操作。
提交表单的示例代码:
@Test
public void submittingForm() throws Exception {
final WebClient webClient = new WebClient();
// Get the first page
final 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.
final HtmlForm form = page1.getFormByName("myform");
final HtmlSubmitInput button = form.getInputByName("submitbutton");
final 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.
final HtmlPage page2 = button.click();
webClient.closeAllWindows();
}
欲了解更多详细信息,请检查:http://htmlunit.sourceforge.net/gettingStarted.html http://htmlunit.sourceforge.net/gettingStarted.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)