How to clear all content in html text box using Selenium?
Last Updated :
23 Jul, 2025
Selenium is one of the most popular tools out there when it comes to automating web applications. Selenium is widely used for automating user interactions like filling out the forms, clicking on buttons, or navigating to a page. Selenium provides a programmable interface where users can write automated scripts and interact with the web browser.
Textboxes are the input boxes in HTML that are used to take input from the users. Text boxes are widely used in web forms for taking inputs such as email ID, password, feedback, mobile numbers, and other information in text.
While we are dealing with web forms it's very common to interact with text boxes and sometimes the textbox, we want to interact with is already filled with some default values, so we have to clear out the content of the text box to use it.
So, in this article, I'll show you a step-by-step tutorial on how to clear all content in an HTML text box using Selenium.
Prerequisites:
Before we learn how to clear all content in the HTML text box using Selenium make sure you have
- Python is installed in your system.
- Selenium library installed using 'pip install selenium'.
Steps to clear all content in the HTML text box using Selenium:
Step 1. Installed the necessary libraries:
We'll need to import the web Driver library from Selenium. Web Driver is an essential component of Selenium which allows us to interact with the web browsers programmatically.
We'll also need to import by class from Selenium Web Driver. By is a class used in selenium that provides a mechanism through which we can easily locate an element on a web page.
Python
# code
from selenium import webdriver
from selenium.webdriver.common.by import By
Step 2. Initializing the Web Driver and navigating to the web page:
In this step we'll need to initialize the Web Driver and navigate to the web page. In this article we'll use Google Chrome Browser.
Python
from selenium import webdriver
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
url="https://www.google.com/search?q=Geeks+For+Geeks" #Replace this with your url
driver.get(url)
In this article we'll clear the content of this textbox using Selenium:
How to clear all content in html text box using Selenium?Step 3. Locating the Text Box:
In this step we'll locate the text box using appropriate Locating Strategy. Locating Strategies are a set of methods used to identify and locate an element on a web page.
For this textbox we'll use NAME attribute to locate the text box in Selenium as it has a name attribute of "q"
To know more about Locating Strategies in Selenium refer Locator Strategies – Selenium Python.
Python
from selenium import webdriver
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
url="https://www.google.com/search?q=Geeks+For+Geeks"
driver.get(url)
textbox=driver.find_element(By.NAME,"q")
Step 4. Clearing the Content In Text Box:
There are three ways to clear the content in Text Box using Selenium. These methods are called upon the located element text box element to clear its content, they are:
- Using Clear() Method
- Using Keys Class
- Using Action class
Using the Clear() Method:
The clear() method provides a simple and straightforward approach to clear the contents of a texts.
Python
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
url="https://www.google.com/search?q=Geeks+For+Geeks"
driver.get(url)
textbox=driver.find_element(By.NAME,"q")
textbox.clear()
Output:
Using the Keys Class:
Keys is a special class in Selenium which is used to stimulate the keyboard interactions in selenium. keys class provides a set of Special Keys which we can use as a single key or as combination with other keys to stimulate keyboard interacts.
Before using the Keys Class we'll have to import keys from selenium webdriver import Keys. Make sure to add this line in the imports.
from selenium.webdriver import Keys
For clearing the content of a textbox we'll have to use the BACKSPACE key
textbox.send_keys(Keys.BACKSPACE)
But sending a single BACKSPACE key will only remove a single character from the input box so we'll use a for loops starting from range 0 to length of textbox and send a BACKSPACE key.
Python
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
url="https://www.google.com/search?q=Geeks+For+Geeks"
driver.get(url)
textbox=driver.find_element(By.NAME,"q")
len_of_textbox=len(textbox.text)
for i in range(len_of_textbox):
textbox.send_keys(Keys.BACKSPACE)
Output:
Using the Action Class:
Action class is a special class in Selenium which allows it perform keyboard and mouse interactions such double clicking on an element, selecting all the text and other complex interactions on a web page.
But before using the Actions class we'll first have to import Action Chains class and Keys class from selenium
from selenium.webdriver import Keys, ActionChains
Step 1. Now first we initialize the Action Chains class.
actions=ActionChains(driver)
Step 2. Then we'll use the actions object to focus on the located textbox using click() method. Click() method is used to click on a located element on a web page.
actions.click(textbox)
Step 3. In this step we'll step send multiple keys to select all the text in the text box and delete it.
- actions.key_down(Keys.CONTROL)- Press the control key.
- actions.send_key("a") - To press A on a keyboard.
- CTRL+ A is shortcut to select on all the text on a section.
- actions.keys_up(Keys.CONTROL) -To release the control Key.
- actions.send_key(Keys.DELETE) -To press the delete key to remove all the selected key.
Python
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import Keys, ActionChains
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
url="https://www.google.com/search?q=Geeks+For+Geeks"
driver.get(url)
textbox=driver.find_element(By.NAME,"q")
actions=ActionChains(driver)
actions.click(textbox)
actions.key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL).send_keys(Keys.DELETE)
actions.perform()
Output:
Conclusion:
Selenium is a useful tool when it comes to automating user interaction and know how to clear content of a html content will sure aid in interacting with web forms. You can clear the content of the html text box using any of the three methods.clear() method provides a simple and straight-forward way to clear all the content of the textbox, Keys class and Action Class are more advanced methods which gives more flexibility to your automation needs. Choose the methods fits your scenario and enhances the reliability of your automation script.
Similar Reads
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING