The submit() vs. click() methods in Selenium

Need to submit a form as part of your test case? Not sure which method to use? We’ve written this guide to help.

Published Categorized as Web & Code

Suppose you’re creating an automation for a test case in Selenium WebDriver, and one of the steps in your automation is to submit a form (as test cases very often do).

If you know your way around Selenium WebDriver, you know that you can either submit the form using the submit() method or click the form’s submission button using the click() method.

But what’s the difference between them — and which one should you use?

In Selenium, both the submit() and the click() methods can be used for submitting a form. The submit() method targets the form element, whereas the click() method targets the submission button.

The submit() method waits for the DOM to load, but the click() method doesn’t. So if you’re using the click() method to submit a form and you need to wait for a certain amount or time or a DOM event, you need to set an explicit pause or wait command.

Use the click() method when your test case explicitly needs to verify what happens when a button is clicked, and the submit() method when your test case is agnostic.

The submit() Method

// Find and submit #my-form
driver.findElement(By.id("my-form")).submit()

The target of the submit() method must be a <form> element within the HTML markup of the page or any of its child elements. With this method, you don’t need to explicitly target the form’s submission button — you’re simply submitting the form.

The keys to making the submit() method work for you are to make sure you’re targeting the right form element (duh!), that the submission button is nested inside that element in the DOM tree, and to carefully consider if you shouldn’t be testing the click of the button instead.

There will be certain scenarios in which you will want to explicitly verify that the button is clickable and functioning, in which case you should probably be using the click() method instead. (For example, as one StackOverflow user points out, if you have code that only fires on the button’s click event rather than the form’s submit event.)

The click() Method

// Find and click #my-form-button
driver.findElement(By.id("my-form-button")).click()

The target of the click() method must be the HTML element that’s acting as the form’s submission button, or any of its children. Technically, that element should be a <button>, although it could also be an <a> element, and web developers often incorrectly create buttons with <div> elements.

So make sure you’re targeting the right HTML element in the DOM tree (or one of its children) depending on the scenario for the test case at hand.

Bottom Line

You can submit a form using the submit() and the click() methods in Selenium. One targets the form element, and the other the form’s button. There are pros and cons to each, but the golden rule is to opt for the click() method whenever you need to test explicitly what happens after the button’s click event.

By Dim Nikov

Editor of Maker's Aid. Part programmer, part marketer. Making things on the web and helping others do the same since the 2000s. Yes, I had Friendster and Myspace.

Leave a comment

Your email address will not be published. Required fields are marked *