- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to delete default values in text field using Selenium?
We can delete default values in the text field with Selenium webdriver. There are multiple ways to do this. We can use the clear() method which resets value present already in edit box or text area field.
We can use the Keys.chord() method along with sendKeys(). The Keys.chord() method helps to press multiple keys simultaneously. It accepts the sequence of keys or strings as a parameter to the method.
To delete default values, it takes, Keys.CONTROL, "a" as parameters. This string is then sent as a parameter to the sendKeys() method. Finally, we have to pass Keys.DELETE to the sendKeys() method.
Let us consider the below edit field where we will delete the input values.
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class DelDefaultVal{ 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(4, TimeUnit.SECONDS); // identify element WebElement l = driver.findElement(By.id("gsc-i-id1")); // input text l.sendKeys("Selenium"); // delete default value with clear() l.clear(); driver.quit() } }
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 DelDefaultValue{ 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(4, TimeUnit.SECONDS); // identify element WebElement l = driver.findElement(By.id("gsc-i-id1")); // input text l.sendKeys("Selenium"); // sending Ctrl+a by Keys.Chord() String s = Keys.chord(Keys.CONTROL, "a"); l.sendKeys(s); // sending DELETE key l.sendKeys(Keys.DELETE); driver.quit() } }
- Related Articles
- How do I send a DELETE keystroke to a text field using Selenium with Python?
- How to include Text object's default values in its serialization using FabricJS?
- How to delete text from document using HTML?
- How to create a text field using JavaFX?
- How can I delete an element in Selenium using Python?
- How to set default Field Value in MySQL?
- How to input text in the text box without calling the sendKeys() using Selenium?
- How to find an element using the “Link Text/Partial Link Text” in Selenium?
- How to add default vertical scaling to a text canvas using Fabric.js?
- How can I consistently remove the default text from an input element with Selenium?
- How to get the text from a website using selenium?
- How to place cursor position at end of text in text input field using JavaScript?
- How to set Selenium Python WebDriver default timeout?
- How to open chrome default profile with selenium?
- How to make an Android Spinner with initial default text using Kotlin?

Advertisements