Friday, February 13, 2015

WebDriver - Clear text from textbox

WebDriver - Clear text from textbox

Clear() Method

Clear() method is used to clear the text entered in a WebElement.

Syntax:
Void clear()

Clear() method doesn’t take any input or doesn’t return any output. Its just executed on the target text entry element.

An example of Clear () method:
Public class Clear{
Public static void main (String[] args){

WebDriver driver = new FirefoxDriver();

driver.get(“http://www.google.com”);

WebElement searchBox =
driver.findElement (By.name(“q”));

searchBox.sendKeys(Keys.chord(Keys.SHIFT, “Selenium Testing”));
searchBox.clear();
}
}
Above, WebElement’s clear() method is used to clear the text after typing “Selenium Testing” into Google search box.
[Source of above example: Selenium WebDriver Practical Guide By Satya Avasarala]

Below are some of the links to help you explore more on clear() method as well as how to clear text from textbox or textarea using WebDriver:
1. Selenium:how to delete single characters from a textbox. [Link]
2. Selenium WebDriver: I want to overwrite value in field instead of appending to it with sendKeys using Java. [Link]
3. What does WebElement.clear() Do to TextBoxes? [Link]
4. Clear text from textarea with selenium. [Link]
5. How do I clear a textbox before writing to it? Using Webdirver C# and there is no way to use Keys.Chord. [Link]
6. How to delete default values in text field using selenium? [Link]