Search code examples
formsinputsubmithtmlunit

Submit Form with nameless input in HtmlUnit


Good morning! I need to post a form, I've already changed all the values inside that form (drop-drown list, text fields , check-boxes, etc), in this way:

final HtmlPage page4 =  webClient.getPage("somepagemakinmemad");
HtmlForm formx= page4.getFormByName("lista_grupos");

changing values like:

HtmlSelect duracion = (HtmlSelect) page4.getElementByName("p_duracion_"+datum.getCrn()+"_1");
    HtmlOption option1 = duracion.getOptionByValue("0029_029");
    duracion.setSelectedAttribute(option1, true);

That is, I am changing the values through obtaining the html element from the page, not through the form (is that okay anyway?).

And then I try to submit the form, through:

<input id="p_guardar" class="boton" type="button" onclick="validaAPG()" value="Guardar">

Which is the "button-like" input where someone clicks when is over with the form.

I have to say, when I print the html code of the form, with "asXml()", I see the form the with selected values I've intended to select.

Question: How can I click on that button and send the form? That input runs a script, when clicked

Thanks everyone, let me know if you need any other kind of info.


Solution

  • You can use below code to click on the button to submit the form.

    HtmlButtonInput button = (HtmlButtonInput)page.getElementById("p_guardar");
    page = button.click();
    

    You dnt need to care what script runs when clicked.If you dont need to run the script remove the attribute.