Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HtmlUnit, how to post form without clicking submit button?

Tags:

java

htmlunit

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.

like image 953
user967639 Avatar asked Sep 27 '11 17:09

user967639


People also ask

Can a form submit without button?

The form can be submitted without using submit button by implementing a specific event attribute or by clicking the link. This task can be done by using the OnClick event attribute or by using the form. submit() method in Javascript.

How do you submit form only once after multiple clicking on submit?

onsubmit = function () { if (allowSubmit) allowSubmit = false; else return false; } })(); (well, as sure-fire as you can get with JS enabled anyway). You could disabled the button as a visual confirmation to the end user that the form can only be submit once too. Very correct.

Which method will get executed on clicking submit button?

When we click on the link, the function submitForm() will get executed. This function will get the element object using DOM getElementById() method by passing the form id to this method, then the form will be submitted by using submit() method.

Is HtmlUnit open source?

HtmlUnit is used as the underlying "browser" by different Open Source tools like WebDriver, WETATOR, jenkins-test-harness, Spring Testing, ... Canoo WebTest, JWebUnit, JSFUnit, Celerity, HtmlUnit was originally written by Mike Bowler of Gargoyle Software and is released under the Apache 2 license.


1 Answers

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();
like image 159
mirovarga Avatar answered Sep 23 '22 17:09

mirovarga