[英]HTMLUnit not clicking a button
我有一个简单的HTML单位代码,该代码可用于按下按钮并在页面上提交表单,但是它不起作用。 这是我的代码:
public static boolean sub(String ref, String zip) throws Exception {
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.getOptions().setRedirectEnabled(true);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getCookieManager().setCookiesEnabled(true);
final HtmlPage page1 = webClient.getPage("http://site.com/");
webClient.waitForBackgroundJavaScript(20000);
final HtmlButton button = page1.getFirstByXPath("//*[@id=\"lookup\"]");
final HtmlTextInput orderField = page1.getFirstByXPath("//*[@id=\"order-number\"]");
final HtmlTextInput zipField = page1.getFirstByXPath("//*[@id=\"order-user-info\"]");
orderField.setValueAttribute(ref);
zipField.setValueAttribute(zip);
final HtmlPage page2 = button.click();
webClient.waitForBackgroundJavaScript(20000);
System.out.println(page2.asText());
webClient.closeAllWindows();
return true;
}
所有这些操作是打印出第一页的文本,但已填充了文本框。如您所见,我尝试等待javascript,但仍然无法正常工作。 任何帮助表示赞赏。
更新:我发现了一些新信息。 看来,当我输入正确的订单号时,它只会显示当前页面并填充文本框,而不是应该重定向到的页面,但是当我输入错误的信息时,它将显示当前页面文本框中填写“错误信息”和“错误信息”。 看来这只是不重定向...
你尝试过使用
webClient.getOptions().setRedirectEnabled(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.