How to Submit a Form using Selenium Webdriver?

Selenium Webdriver provides a function called submit() which is used to submit a form.
If you ever noticed that login section of a Website is usually wrapped under form tag. Let’s take an example of a Facebook login page, if you look at the HTML code of it, then you would find that Username & Password textbox including Login button is wrapped under the form tag.
To submit a form using Selenium Webdriver, first you need to identify the form. You can use any web element of that form to identify it. In below code, we will use password text box to identify the form.

SubmitFormSample

Script to submit a login form is as follows :-

//Enter username
driver.findElement(By.id("user")).sendKeys("contact.automateapps@gmail.com");

//Enter password
driver.findElement(By.id("pass")).sendKeys("password");

//submit form
driver.findElement(By.id("pass")).submit();

/* In order to submit a form, you can use any element of a form. 
WebDriver will find the login form using that element. 
In the above code, id of password input box is used to identify a form.

*/

NOTE: It is not mandatory to call submit(), if you want to submit the form. You can also click on Submit button to do the same. It’s up to you which option you want to implement in your script.

If you really like the information provided above, please don’t forget to hit a like on Facebook Page, you can also leave a comment.

Leave a Reply

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