HTMLUnit,如何在不单击提交按钮的情况下发布表单? [英] HtmlUnit, how to post form without clicking submit button?
问题描述
我知道在HtmlUnit中我可以fireEvent
在表单上提交,它将被发布.但是,如果我禁用了javascript并想使用一些内置函数发布表单怎么办?
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?
我已经检查了Javadoc,但没有找到任何方法来执行此操作.奇怪的是,HtmlForm中没有这样的功能...
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...
我阅读了htmlunit页面上的javadoc和教程,并且知道可以使用getInputByName()
并单击它.有时有些表格没有提交类型按钮
甚至有这样的按钮,但没有名称属性.
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.
在这种情况下我正在寻求帮助,这就是为什么我使用fireEvent
但它并不总是有效的原因.
I am asking for help in such situation, this is why i am using fireEvent
but it does not always work.
推荐答案
您可以使用临时"提交按钮:
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();
这篇关于HTMLUnit,如何在不单击提交按钮的情况下发布表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!