javaselenium-webdriverhtmlunit-driver

Submit form without button in selenium


On the page I want to test I have a form like this:

<form method="post" action="target.html">
    <input id="myInputField" />
</form>

A normal use-case here would be that the user enters some text and then hits enter. For testing I tried sending the Special-Key "enter" but that results in an exception:

org.openqa.selenium.WebDriverException: Cannot locate element used to submit form

Is there a way to submit a form without having a button present? I am testing headless with HtmlUnitDriver.


Solution

  • With a little workaround I got it to work: Creating the submit button but setting the display to none does the trick. But I am still searching for a better solution

    <form ...>
    ...
    <input type="submit" style="display:none" />
    ...
    </form>
    

    Now the send()-Method can find the submit-Element.