How to submit a form in Selenium?

Software TestingAutomation TestingSelenium Web Driver

We can submit a form in Selenium by the following methods −

  • submit()

  • click()

Code Implementation with submit() method.

Example

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class SubmitScripting {
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "<<web url with a form submit feature>>";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      // submit method to submit the form
      driver.findElement(By.id("<<id expression>>")).submit();
      driver.close(); 
   }
}

Code Implementation with method click().

Example

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class ClickScripting {
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "<<web url with a form submit feature>>"; driver.get(url);
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      // click method to submit the form
      driver.findElement(By.id("<<id expression>>")).click();
      driver.close();
   }
}
raja
Published on 10-Jun-2020 15:44:22
Advertisements