How to Delete Single Characters from a Textbox Using Selenium

How to Delete Single Characters from a Textbox Using Selenium

3 July 2024 Stephan Petzl Leave a comment QA

When working with Selenium for web automation, you might encounter a scenario where you need to delete specific characters from a textbox. For instance, you may need to delete the last two characters from a string like “ABCD” to get “AB”. This article provides a clear and practical guide to achieve this using Selenium.

Approach 1: Using ASCII Code

If you are using Java, you can delete a character from a textbox by sending the ASCII backspace character. Here’s how you can do it:


This approach is straightforward and relies on using the Unicode representation of the backspace character.

Approach 2: Using Selenium Keys

While the ASCII method is valid, using predefined constants in Selenium can make your code more readable and maintainable. The Selenium library provides a Keys class that includes a constant for the backspace key. Here’s how you can use it:


This method enhances code readability and aligns with standard practices in Selenium-based automation.

Practical Example

Let’s consider a practical example where we need to delete the last two characters from a textbox:

    WebElement element = driver.findElement("textboxId"));

In this example, we locate the textbox using its ID and then send two backspace keys to delete the last two characters.


Both approaches are effective for deleting characters from a textbox using Selenium. The choice between them depends on your preference for code readability and maintainability. Using predefined constants from the Keys class is generally recommended for clarity.

Enhancing Your Testing with Repeato

If you’re looking to streamline your testing process further, consider using Repeato, a no-code test automation tool for iOS and Android. Repeato allows you to create, run, and maintain automated tests for your apps with ease. Its computer vision and AI capabilities make it particularly fast to edit and run tests, ensuring your quality assurance processes are efficient and effective.

For more details, visit our documentation or contact us for support.

Like this article? there’s more where that came from!