How to clear the text entered in Selenium with python?

Selenium Web DriverAutomation TestingSoftware Testing

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
raja
Published on 29-Jul-2020 11:22:44
Advertisements