【问题标题】:HtmlUnit, how to post form without clicking submit button?HtmlUnit,如何在不点击提交按钮的情况下发布表单?
【发布时间】:2011-09-27 17:54:30
【问题描述】:

我知道在 HtmlUnit 中我可以 fireEvent 在表单上提交,它将被发布。但是如果我禁用了 javascript 并想使用一些内置函数发布表单怎么办?

我检查了 javadoc 并没有找到任何方法来做到这一点。奇怪的是HtmlForm中没有这个功能……


我阅读了 htmlunit 页面上的 javadoc 和教程,我知道我可以使用 getInputByName() 并单击它。但有时有些表单没有提交类型按钮 甚至有这样的按钮,但没有名称属性。

我在这种情况下寻求帮助,这就是我使用 fireEvent 的原因,但它并不总是有效。

【问题讨论】:

  • 我建议使用HttpURLConnection 并按照here 列出的说明进行操作。或者使用 Apache 的 HttpClient 类。
  • 再次检查 JavaDoc :) 或者介绍 -> 入门部分,正如 Ransom Briggs 所建议的那样。我不会采用 mrkhrts 方法......它太低级了

标签: java htmlunit


【解决方案1】:

您可以使用“临时”提交按钮:

WebClient client = new WebClient();
HtmlPage page = client.getPage("http://stackoverflow.com");

// create a submit button - it doesn't work with 'input'
HtmlElement button = page.createElement("button");
button.setAttribute("type", "submit");

// append the button to the form
HtmlElement form = ...;
form.appendChild(button);

// submit the form
page = button.click();

【讨论】:

  • 你我的朋友真是个天才!一个星期以来一直在解决这个问题!
  • 就是解决方案,我以前用过很多瓷砖,从来没出过问题。
  • imho 这很麻烦,可能会有一些副作用。除非这是您的测试场景,否则您应该尝试尽可能接近用户行为。请参阅我的答案以获取一些“本机”解决方案。
  • 你Mirovaraga先生是个天才。现在也已经为此工作了两周。非常感谢 - 我欠你在曼彻斯特的一家顶级餐厅过夜
【解决方案2】:
WebRequest requestSettings = new WebRequest(new URL("http://localhost:8080/TestBox"), HttpMethod.POST);

// Then we set the request parameters
requestSettings.setRequestParameters(Collections.singletonList(new NameValuePair(InopticsNfcBoxPage.MESSAGE, Utils.marshalXml(inoptics, "UTF-8"))));

// Finally, we can get the page
HtmlPage page = webClient.getPage(requestSettings);

【讨论】:

    【解决方案3】:
    final HtmlSubmitInput button = form.getInputByName("submitbutton");
    final HtmlPage page2 = button.click()
    

    来自the htmlunit doc

    @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();
    }
    

    【讨论】:

    • OP 已编辑,现在它说没有提交按钮。
    【解决方案4】:

    如何使用内置的 javascript 支持?只需在该表单上触发提交事件:

    HtmlForm form = page.getForms().get(0);
    form.fireEvent(Event.TYPE_SUBMIT);
    

    代码假设您要在网站上提交第一个表单。

    如果提交将您转发到另一个站点,只需将响应链接到页面变量:

    HtmlForm form = page.getForms().get(0);
    page = (HtmlPage) form.fireEvent(Event.TYPE_SUBMIT).getNewPage();
    

    【讨论】:

      【解决方案5】:

      虽然这个问题有很好的有效答案,但似乎没有一个能很好地模拟实际的用户行为。
      想一想:当没有按钮可以点击时,人类会做什么?很简单,你按回车(回车)。这就是它在 HtmlUnit 中的工作方式:

      // assuming page holds your current HtmlPage
      HtmlForm form = page.getFormByName("yourFormName");
      HtmlTextInput input = form.getInputByName("yourTextInputName");
      // type something in
      input.type("here goes the input");
      // now hit enter and get the new page
      page = (HtmlPage) input.type('\n');
      

      请注意,input.type("\n"); input.type('\n'); 相同!

      当您禁用 javascript 执行并且没有可用的提交按钮时,此方法有效。


      恕我直言您应该首先考虑如何您希望提交表单。你想测试什么场景?用户点击返回可能与单击某个按钮(可能有一些 onClick)不同。而通过 JavaScript 提交表单可能是另一个测试用例。

      当您发现这一点时,请从其他答案(当然还有这个)中选择提交表单的适当方式。

      【讨论】:

        猜你喜欢
        • 2016-11-28
        • 1970-01-01
        • 2013-04-10
        • 1970-01-01
        • 1970-01-01
        • 2016-01-04
        • 1970-01-01
        • 2021-12-20
        • 2016-08-29
        相关资源
        最近更新 更多