clear() Method

Clear method will erase the text in the input fields.  It is like an sendKeys method action. We can use Backspace also using char sequence from the keyboard.  But this method will be very easy to apply.

JAVA API Syntax : void clear()

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@xxx");  

//clear the texts in the input field
driver.findElement(By.id("email")).clear
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
driver.findElement(By.id("pass")).clear;  
     
//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")).clear();
}
}
Socialize It and Share the post with your friends
SOCIALIZE IT →
FOLLOW US →
SHARE IT →