Click and Submit method will be performing the same actions when submitting the button on the form of the webpage. But, click method performs various actions like clicking and draging, moving the webelement from one location to another location. Whereas, submit method performs on the submitting the button web element on the webpage. For example, user can submit the login credentials for user id and password in facebook, gmail, etc.,
JAVA API Syntax : void submit()
NoSuchElementException will be thrown when the element is not found by the web driver.
Code:
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class clearinputs { public static void main(String args[]) { //Launching the firefoxbrowser WebDriver driver = new FirefoxDriver(); //Implicit wait, this method is used to wait for the webelement to load driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //Navigate to FB page driver.get("http://www.facebook.com"); //typing the texts using sendkeys method for user id and password driver.findElement(By.id("email")).sendKeys("xxxxx@gmail.com"); driver.findElement(By.id("pass")).sendKeys("xxxxx@123"); //performing submitting actions on the login button driver.findElement(By.id("loginbutton")).submit(); //Navigate to google search page driver.navigate().to("https://www.google.com"); //Enter the text using SHIFT keys in search box WebElement gsearch = driver.findElement(By.name("q")); gsearch.sendKeys(Keys.chord(Keys.SHIFT,"www.qafreaks.com")); //erasing again the input in the search box driver.findElement(By.name("btnG")).submit(); } }