Submit Web Form with WebDriver in Java


How to submit Web Page Form with Selenium WebDriver in Java?



You can follow this tutorial to enter input data and submit a Web page form with Selenium WebDriver.

Several methods from Selenium WebDriver classes will be used:

  • driver.get() - Loads a new Web page from the given URL in the current browser window. This is done using an HTTP GET operation, and the method will block until the load is complete.
  • driver.findElement() - Searches and returns the first HTML element that matches the given criteria.
  • By.tagName() - Returns a match criteria to search for HTML elements.
  • input.sendKeys() - Simulates a user typing data into an INPUT element of a FORM element.
  • form.submit() - Submits the FORM element to the Website.

1. Enter the following program,

// Copyright (c) 
import org.openqa.selenium.*;
public class FormSubmit {
   public static void main(String[] args) {
      WebDriver driver = WebDriverLoader.load(args[0]);

	  // get the first "form" element in the page.
      WebElement form = driver.findElement(By.tagName("form"));

	  // get the input element of the form.
	  WebElement query = form.findElement("Q"));
	  // submit form
	  String url = driver.getCurrentUrl();

      System.out.println("Test Case - Submit a search:");
      System.out.println("   Actual Value: "+url);
      System.out.println("   Expected Value: ..Q=selenium...");
      if (url.contains("Q=selenium")) {
         System.out.println("   Test Result: Passed");
      } else {
         System.out.println("   Test Result: Failed");

	  // pause for 1 minute before closing
	  try {
      } catch (Exception e) {

2. Compile and run it with the Selenium Client JAR file.

C:\fyicenter> javac -classpath \
   .;\fyicenter\selenium\java\client-combined-3.141.59.jar \

C:\fyicenter> java -classpath \
   \fyicenter\selenium\java\libs\commons-exec-1.3.jar \
   FormSubmit Chrome
Test Case - Submit a search:
   Actual Value:
   Expected Value: ..Q=selenium...
   Test Result: Passed

As you can see, we are able to enter a search criteria and submit the FORM element without any problem with Chrom WebDriver.


⇒ JavascriptExecutor to Run JavaScript in Java

⇐ Retrieve Web Form Elements with WebDriver in Java

⇑ Using Selenium WebDriver Client Java API

⇑⇑ Selenium Tutorials

2020-01-04, 1039👍, 0💬