How to reset or clear an edit box in Selenium?



We can reset or clear an edit box in Selenium with the help of clear() method.

Code Implementation with clear().

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 ResetText {
   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.tutorialspoint.com/index.htm";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
      //entering text in the edit box
      driver.findElement(By.cssSelector("#gsc-i- id1")).sendKeys("Selenium");
      Thread.sleep(1000);
      // resetting text from the edit box with clear()
      driver.findElement(By.cssSelector("#gsc-i-id1")).clear();
      driver.close();
   }
}

Advertisements