How to clear the text entered in Selenium with python?

We can enter text on any field in Selenium. After entering the text, we may need to remove or clear the text we entered in that field. This interaction with the web elements can be achieved with the help of clear() method.

Thus a clear() method is used to clear or reset an input field belonging to a form/ edit box. It replaces the text content on that particular element of the page.


driver.find_element_by_xpath("//input[class ='gsc-search']").clear()


Code Implementation with clear() method.

from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then #invoke actual browser
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
# to maximize the browser window
#get method to launch the URL
#to refresh the browser
# identifying the edit box and entering text in edit box
driver.find_element_by_xpath("//*[@class='gsc-input']"). send_keys("Selenium")
# clears the content
#to close the browser

Updated on: 29-Jul-2020

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started