Submitting Form Using submit() Method Of Selenium WebDriver

You need to submit form in selenium very frequently as most of the sites contains forms. 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. Selenium webdriver submit form can be done using submit() method. submit() method works same as clicking on submit button for selenium submit form task.


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 in selenium 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 = null;
 
 
  @BeforeTest
  public void setup() throws Exception { 
  // set geckodriver path.
  System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");
  //initialize firefox driver.
  driver = new FirefoxDriver();
         driver.manage().window().maximize();
         driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
         driver.get("http://only-testing-blog.blogspot.com/2014/05/form.html"); 
  } 
 
  @AfterTest
  public void tearDown() throws Exception { 
   driver.quit();
     } 
  
  @Test
  public void LogIn_Test(){
   driver.findElement(By.xpath("//input[@name='FirstName']")).sendKeys("MyFName");
   driver.findElement(By.xpath("//input[@name='LastName']")).sendKeys("MyLName");
   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.
   driver.findElement(By.xpath("//input[@name='Company']")).submit();
   String alrt = driver.switchTo().alert().getText();
   driver.switchTo().alert().accept();
   System.out.println(alrt);
  }
}

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

7 comments:

  1. Hi, thanks for the great tutorial.

    I wonder, what does this line of code do?
    driver.switchTo().alert().accept()

    I tried without it and got the same result.

    ReplyDelete
  2. Hi Fabian Baehrendtz,

    Alert message will be closed in this line of code
    driver.switchTo().alert().accept()

    ReplyDelete
  3. I get a huge error "// Compiled from ErrorHandler.java (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?

    ReplyDelete
  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.

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

    ReplyDelete
  6. Good one. thanks for sharing this information. But i think since .click provide more advantage and can be used in all cases. This one even can confuse people who are maintaining the code as people will think why the person has used another element of the form to use submit button.

    Good to know but not the best thing(submit) to use in my opinion

    ReplyDelete
  7. To close the alert you need to pass that above command

    ReplyDelete