简体   繁体   English

HtmlUnit,如何在不单击提交按钮的情况下发布表单?

[英]HtmlUnit, how to post form without clicking submit button?

I know that in HtmlUnit i can fireEvent submit on form and it will be posted. 我知道在HtmlUnit中我可以在表单上提交fireEvent并将其发布。 But what If I disabled javascript and would like to post a form using some built in function? 但是如果我禁用了javascript并想使用一些内置函数发布表单呢?

I've checked the javadoc and haven't found any way to do this. 我已经检查了javadoc并且没有找到任何方法来做到这一点。 It is strange that there is no such function in HtmlForm... 奇怪的是HtmlForm中没有这样的功能......


I read the javadoc and tutorial on htmlunit page and I Know that i can use getInputByName() and click it. 我在htmlunit页面上阅读了javadoc和教程,我知道我可以使用getInputByName()并单击它。 BuT sometimes there are forms that don't have submit type button or even there is such button but without name attribute. BuT有时会有没有提交类型按钮的表单,甚至有这样的按钮但没有name属性。

I am asking for help in such situation, this is why i am using fireEvent but it does not always work. 我在这种情况下寻求帮助,这就是我使用fireEvent但它并不总是有效的原因。

You can use a 'temporary' submit button: 您可以使用“临时”提交按钮:

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();
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);
final HtmlSubmitInput button = form.getInputByName("submitbutton");
final HtmlPage page2 = button.click()

From the htmlunit doc 来自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();
}

How about getting use of built-in javascript support? 如何使用内置的JavaScript支持? Just fire submit event on that form: 只需在该表单上触发提交事件:

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

The code supposes you want to submit first form on the site. 代码假设您要在网站上提交第一个表单。

And if the submit forwards you to another site, just link the response to the page variable: 如果提交将您转发到另一个站点,只需将响应链接到页面变量:

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

Although this question has good and working answers none of them seems to emulate the acutal user behaviour quite well. 虽然这个问题具有良好且有效的答案,但它们似乎都没有能够很好地模仿真实的用户行为。
Think about it: What does a human do when there's no button to click? 想想看:当没有按钮点击时,人类做了什么? Simple, you hit enter (return). 很简单,你点击回车(返回)。 And that's just how it works in HtmlUnit: 这就是它在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');

Note that input.type("\\n"); 注意input.type("\\n"); is not the same as input.type('\\n'); 一样的input.type('\\n'); !

This works when you have disabled javascript exection and when there's no submit button available. 当您禁用了javascript执行并且没有可用的提交按钮时,此方法有效。


IMHO you should first think of how you want to submit the form. 恕我直言 ,你首先应该想到的你要如何提交表单。 What scenario is it that you want to test? 你想测试什么场景? A user hitting return might be a different case that clicking some button (that might have some onClick). 用户点击返回可能是单击某个按钮(可能有一些onClick)的不同情况。 And submitting the form via JavaScript might be another test case. 通过JavaScript提交表单可能是另一个测试用例。

When you figured that out pick the appropriate way of submitting your form from the other answers (and this one of course). 当你想出那个从其他答案中提取你的表格的适当方式时(当然这是一个)。

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

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