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

发布于 2024-12-06 12:59:05 字数 349 浏览 2 评论 0原文

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

我检查了javadoc,但没有找到任何方法来做到这一点。奇怪的是,HtmlForm 中没有这样的功能...


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

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

I know that in HtmlUnit i can fireEvent submit on form and it will be posted. But what If I disabled javascript and would like to post a form using some built in function?

I've checked the javadoc and haven't found any way to do this. It is strange that there is no such function in HtmlForm...


I read the javadoc and tutorial on htmlunit page and I Know that i can use getInputByName() and click it. BuT sometimes there are forms that don't have submit type button
or even there is such button but without name attribute.

I am asking for help in such situation, this is why i am using fireEvent but it does not always work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

多情癖 2024-12-13 12:59:05

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

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

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();
疑心病 2024-12-13 12:59:05
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);
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);
怕倦 2024-12-13 12:59:05
final HtmlSubmitInput button = form.getInputByName("submitbutton");
final HtmlPage page2 = button.click()

来自 htmlunit 文档

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

From 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();
}
长发绾君心 2024-12-13 12:59:05

使用内置的 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();

How about getting use of built-in javascript support? 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();
凉薄对峙 2024-12-13 12:59:05

尽管这个问题有很好且有效的答案,但它们似乎都不能很好地模拟实际的用户行为。
想一想:当没有按钮可以点击时,人类会做什么?很简单,你按回车键(回车)。这就是它在 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 提交表单可能是另一个测试用例。

当您弄清楚这一点后,从其他答案中选择提交表单的适当方式(当然还有这个)。

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:

// 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"); is not the same as input.type('\n');!

This works when you have disabled javascript exection and when there's no submit button available.


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). And submitting the form via JavaScript might be another test case.

When you figured that out pick the appropriate way of submitting your form from the other answers (and this one of course).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文