Clear text from textarea with selenium.


We can clear text from a text area with Selenium. We shall use the clear method to remove the content from a text area or an edit box. First we shall identify the text area with the help of any locator.

A text area is identified with textarea tagname in the html code. Let us input some text inside the below text area, then clear the text.

Example

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TextAreaClear{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("http://www.uitestpractice.com/Students/Form");
      // identify element
      WebElement m = driver.findElement(By.id("comment"));
      // enter text
      m.sendKeys("Selenium");
      // obtain value entered in text area
      System.out.println("Value entered: " + m.getAttribute("value"));
      // clear text area
      m.clear();
      // obtain value entered in text area after clear applied
      System.out.println("Value after clear(): " + m.getAttribute("value"));
      driver.quit();
   }
}

Output

Updated on: 30-Nov-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements