Delivered by FeedBurner

Submitting Form Using submit() Method Of Selenium WebDriver

You will find many forms In any software web application like Contact Us form, New User Registration Form, Inquiry Form, LogIn Form etc.. Supposing you are testing one software website where you have to prepare Login form submission test case In selenium webdriver then how will you do It? Simplest way Is described In THIS POST. If you will see In that example post, we have used .click() method to click on Login button.

Selenium Webdriver software testing tool has one special method to submit any form and that method name Is submit(). submit() method works same as clicking on submit button.

When to use .click() method
You can use .click() method to click on any button of software web application. Means element's type = "button" or type = "submit", .click() method will works for both.

When to use .submit() method
If you will look at firebug view for any form's submit button then always It's type will be "submit" as shown In bellow given Image. In this case, .submit() method Is very good alternative of .click() method.

Final Notes :
1. If any form has submit button which has type = "button" then .submit() method will not work.
2. If button Is not Inside <form> tag then .submit() method will not work.

Now let us take a look at very simple example where I have used .submit() method to submit form on software web page. In bellow given example, I have not used .click() method but used .submit() method with company name field. Run bellow given example In eclipse with testng and verify the result.
package Testng_Pack;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Form_Submit {
WebDriver driver = new FirefoxDriver();
  public void setup() throws Exception { 
         driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  public void tearDown() throws Exception { 
  public void LogIn_Test(){
   driver.findElement(By.xpath("//input[@name='EmailID']")).sendKeys("My Email ID");
   driver.findElement(By.xpath("//input[@name='MobNo']")).sendKeys("My Mob No.");
   driver.findElement(By.xpath("//input[@name='Company']")).sendKeys("My Comp Name");
   //To submit form.
   //You can use any other Input field's(First Name, Last Name etc.) xpath too In bellow given syntax.
   String alrt = driver.switchTo().alert().getText();

Above example will simply submit the form and retrieve submission alert to print. So this way we can use webdriver's submit method to submit any form. You can try different form for your better understanding.


  1. Hi, thanks for the great tutorial.

    I wonder, what does this line of code do?

    I tried without it and got the same result.

  2. Hi Fabian Baehrendtz,

    Alert message will be closed in this line of code

  3. I get a huge error "// Compiled from (version 1.6 : 50.0, super bit)
    public class org.openqa.selenium.remote.ErrorHandler {

    // Field descriptor #13 Ljava/lang/String;
    private static final java.lang.String MESSAGE = "message";

    // Field descriptor #13 Ljava/lang/String;
    private static final java.lang.String SCREEN_SHOT = "screen"; ..... so on"
    Can u pls help me out?

  4. Hi..
    I tried running this program using testing but not able to. It gives an error stating 'Launching Form_Submit' has encountered an error. An internal error occurred during:" Launching Form_Submit".
    Please help me out.

  5. What is this should be taken by excel. Your example is so easy even kids can do it. pls give some data driven examples