简体   繁体   English

HTMLUnit没有点击按钮

[英]HTMLUnit not clicking a button

I have a simple HTML unit code that is used to press a button and submit a form on a page, but it doesn't want to work. 我有一个简单的HTML单位代码,该代码可用于按下按钮并在页面上提交表单,但是它不起作用。 This is my code: 这是我的代码:

    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;
}

All this does is print out the text of the first page, but with the text boxes filled in. As you can see, I tried waiting for javascript, it still doesn't work. 所有这些操作是打印出第一页的文本,但已填充了文本框。如您所见,我尝试等待javascript,但仍然无法正常工作。 Any help is appreciated. 任何帮助表示赞赏。

UPDATE: I found at some new info. 更新:我发现了一些新信息。 It seems that when I enter an order number that is correct, it just shows the current page with the text boxes filled in, instead of the page it is suposed to redirect to, but when I enter wrong info, it shows the current page with the text boxes filled in AND the "Wrong Info" error message. 看来,当我输入正确的订单号时,它只会显示当前页面并填充文本框,而不是应该重定向到的页面,但是当我输入错误的信息时,它将显示当前页面文本框中填写“错误信息”和“错误信息”。 It seems that this is just not redirecting... 看来这只是不重定向...

你尝试过使用

webClient.getOptions().setRedirectEnabled(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM