How do I send a DELETE keystroke to a text field using Selenium with java? Last Updated : 16 Oct, 2024 Comments Improve Suggest changes Like Article Like Report Sending a DELETE keystroke to a text field using Selenium with Java is a common task when automating web form inputs. Whether you're testing the behavior of input fields or simulating user actions, it's important to know how to interact with text fields effectively. Selenium provides a simple way to send keystrokes like DELETE or BACKSPACE to edit or remove text within web elements. In this guide, we will explain how to send DELETE keystrokes to text fields in Selenium WebDriver using JavaExample to send a DELETE keystroke to a text field using Selenium with javaHere is an example of how to to send a DELETE keystroke to a text field using Selenium with Java: DeleteKeystrokeExample.java package seleniumpractice; 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; public class DeleteKeystrokeExample { public static void main(String[] args) { // Set the path for the ChromeDriver executable System.setProperty( "webdriver.chrome.driver", "path/to/chromedriver"); // Update with your // ChromeDriver path // Initialize WebDriver WebDriver driver = new ChromeDriver(); try { // Open the GeeksforGeeks website driver.get("https://www.geeksforgeeks.org/"); // Locate the search field on the homepage WebElement searchField = driver.findElement( By.xpath("//input[@type='text']")); // Input some text into the search field searchField.sendKeys("Data Structures"); // Print the current text in the search field System.out.println( "Before adding extra text: " + searchField.getAttribute("value")); // Add additional text before deleting searchField.sendKeys(" in Java"); // Print the updated text after adding extra // text System.out.println( "After adding text: " + searchField.getAttribute("value")); // Send the DELETE keystroke (backspace to // delete the last character) searchField.sendKeys(Keys.BACK_SPACE); // Print the updated text in the search field // after deletion System.out.println( "After DELETE: " + searchField.getAttribute("value")); } finally { // Close the browser driver.quit(); } } } ExplanationPackage : This code is part of the seleniumpractice package.Imports : This imports are brings in necessary selenium classes.Class Declaration : The class is named as DeleteKeystrokeExample.Main Method : The main method is the entry point of the program where the execution begins.WebdriverManager : Automatically handles the setup of the ChromeDriver.ChromeDriver : An instance of ChromeDriver is created., which opens a new chrome browser window.try : This begins a try block to handle exception that may occur during execution.Navigate URL : Defines the navigates the browser to the GeeksforGeeks website.Locator the Element : This search input is located using XPath expression that targets the <input> element with type='text' . This returns a webElement.searchField.sendKeys("Data Structures"); : The string "Data Structure" is sent to the search field, simulating user input.System.out.println("Before DELETE: " + searchField.getAttribute("value")); : This line prints the current value in the search field to the console, which should be "Data Structures".Keys.BACK_SPACE : A BACK_SPACE keystroke is sent to the search field, deleting the last character from the next.System.out.println("After DELETE: " + searchField.getAttribute("value")); This line prints the updated value in the search field to the console. After sending the BACK_SPACE key, it should "Data Structure".finally : This finally blocks ensure that the browser is closed regardless of whether an exception occurred, driver.quit( ) shuts down the browser and ends the WebDriver session.Outputoutput()ConclusionUsing Selenium WebDriver with Java, sending DELETE keystrokes to a text field is straightforward and allows for better control over form input automation. By leveraging the Keys class in Selenium, you can simulate user interactions like deleting text efficiently. Mastering this technique can help you improve test automation scripts and ensure that your web applications handle text inputs as expected. Comment More infoAdvertise with us Next Article How do I send a DELETE keystroke to a text field using Selenium with java? S sunilcjd8i Follow Improve Article Tags : Software Testing Similar Reads How to upload a file in Selenium java with no text box? Uploading a file in Selenium Java without a visible text box can be easily handled using the sendKeys() method. Even if the file input field is hidden or styled as a button, Selenium allows you to interact with it by providing the file path directly. This method simplifies the process of file upload 3 min read How to extract text from a web page using Selenium java and save it as a text file? Extracting text from a web page using Selenium in Java is a common requirement in web automation and scraping tasks. Selenium, a popular browser automation tool, allows developers to interact with web elements and retrieve data from a webpage. In this article, we will explore how to extract text fro 3 min read How to click the 'Ok' button inside an alert window with a Java Selenium command? An alert window is a pop-up box shown over the webpage to convey error messages using the alert() function of JavaScript. It has only one button "OK" which the user must press to continue browsing the webpage. A person might automate the website testing process and stumble upon pages that display al 3 min read How to Drag and Drop an Element using Selenium WebDriver in Java? Selenium is an open-source web automation tool that supports many user actions to perform in the web browser. Automating a modern web page that has a drag and drop functionality and drag and drop is used to upload the files and so many user activities. so to perform the drag and drop actions the sel 2 min read How to Handle Alert in Selenium using Java? Imagine filling out a form online and accidentally missing some information. You only know if you made a mistake if the website tells you somehow, like with a pop-up message. This article explains what those pop-up messages are called in Selenium (alerts) and how to deal with them in your automated 5 min read How to input text in the text box without calling the sendKeys() using Selenium java? When automating web applications using Selenium WebDriver, the standard approach to input text into a text box is by using the sendKeys() method. However, there are scenarios where you might want to input text without calling sendKeys(). This can be achieved using JavaScriptExecutor in Selenium. Jav 3 min read How to automate google Signup form in Selenium using java? For any QA engineer or developer, automating the Google Signup form with Selenium may be a hard nut to crack. Also, as the needs are increasing toward automated testing, in this article, we will learn how to deal with a complicated web form like Google Signup. We will show you how to automate the Go 4 min read How to Take a Screenshot in Selenium WebDriver Using Java? Selenium WebDriver is a collection of open-source APIs used to automate a web application's testing. To capture a screenshot in Selenium, one must utilize the Takes Screenshot method. This notifies WebDriver that it should take a screenshot in Selenium and store it. Selenium WebDriver tool is used t 3 min read How do I pass options to the Selenium Chrome driver using java? Selenium WebDriver is a widely used tool for automating web browsers. When working with Selenium ChromeDriver, passing custom options to the browser allows you to tailor the browsing experience based on your test requirements. Whether you're running tests in headless mode, disabling notifications, o 2 min read How to download File in Selenium Using java Automating the file download process is essential in web automation testing, especially for validating functionalities involving document downloads such as PDFs, images, or CSV files. However, Selenium WebDriver doesnât directly handle file downloads. To overcome this limitation, we can configure th 2 min read Like