Selenium Webdriver submit() vs click().

SeleniumAutomation TestingTesting Tools

The click() and submit() functions are quite similar in terms of functionalities. However there are minor differences. Let us discuss some differences between them.

The submit() function is applicable only for <form> and makes handling of form easier. It can be used with any element inside a form. The click() is only applicable to buttons with type submit in a form.

The submit() function shall wait for the page to load however the click() waits only if any explicit wait condition is provided. If a form has a submit of type button, the submit() method cannot be used. Also, if a button is outside <form>, then submit() will not work.

Thus we see that click() works for both type buttons irrespective of the fact that the button is inside or outside of <form>. Let us take up the below form for implementation.

Example

Code Implementation with submit().

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class SubmitForm{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://www.facebook.com/";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      // identify elements
      driver.findElement(By.id("email")).sendKeys("abc@gmail.com");
      driver.findElement(By.id("pass")).sendKeys("123456");
      // submitting form with submit()
      driver.findElement(By.id("pass")).submit();
      driver.quit()
   }
}

Example

Code Implementation with click().

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class ClickForm{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://www.facebook.com/";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      // identify elements
      driver.findElement(By.id("email")).sendKeys("abc@gmail.com");
      driver.findElement(By.id("pass")).sendKeys("123456");
      // submitting form with click()
      driver.findElement(By.name("login")).click();
      driver.quit()
   }
}
raja
Published on 28-Aug-2020 06:35:10
Advertisements