A normal use-case he" /> A normal use-case he" /> A normal use-case he"/>

Submit form without button in selenium

659 Views Asked by At

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.

2

There are 2 best solutions below

0
fuzzi On BEST ANSWER

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.

3
M.K On

Have you also tried

.sendKeys(Keys.RETURN)

instead of .ENTER?

If this doesn't work I think the answer of @fuzzi is the best way or take a look at this!