How to clear the text entered in Selenium with python?

Selenium Web DriverAutomation TestingSoftware Testing

React Full Stack Web Development With Spring Boot

67 Lectures 4.5 hours

The Complete Selenium WebDriver with Java Course

192 Lectures 20 hours

Fundamentals of React and Flux Web Development

48 Lectures 10.5 hours

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.

Syntax

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

Example

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
driver.maximize_window()
#get method to launch the URL
driver.get("https://www.tutorialspoint.com/current_affairs.htm")
#to refresh the browser
driver.refresh()
# identifying the edit box and entering text in edit box
driver.find_element_by_xpath("//*[@class='gsc-input']"). send_keys("Selenium")
# clears the content
driver.find_element_by_xpath("//*[@class='gsc-input']").clear()
#to close the browser
driver.close
Updated on 29-Jul-2020 07:53:15