- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I send a DELETE keystroke to a text field using Selenium with Python?
We can send a DELETE keystroke to a text field using Selenium webdriver with Python. First of all, we have to identify the text field with the help of any locators like xpath, css, id, and so on.
We can enter a text in the text field with the send_keys method. The value to be entered is passed as parameter to the method. To delete a key, we can pass Keys.BACKSPACE as a parameter to the send_keys method.
Syntax
l = driver.find_element_by_id("gsc−i−id1") l.send_keys("Sel") l.send_keys(Keys.BACKSPACE)
To delete all the keys entered simultaneously, we have to pass CTRL+A and BACKSPACE as parameters to the send_keys method.
Syntax
l = driver.find_element_by_id("gsc−i−id1") l.send_keys("Sel") l.send_keys(Keys.CONTROL + 'a', Keys.BACKSPACE)
Also, to use the Keys class, we have to add the import statement from selenium.webdriver.common.keys import Keys in our code.
Example
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time #set geckodriver.exe path driver = webdriver.Firefox(executable_path="C:\geckodriver.exe") driver.implicitly_wait(0.5) driver.get("https://www.tutorialspoint.com/about/about_careers.htm") #identify element and enter text l = driver.find_element_by_id("gsc−i−id1") l.send_keys("Sel") #delete a key l.send_keys(Keys.BACKSPACE) print("Value after deleting a single key") print(l.get_attribute('value')) #wait for some time time.sleep(0.8) #delete all keys at once l.send_keys(Keys.CONTROL + 'a', Keys.BACKSPACE) print("Value after deleting entire key") print(l.get_attribute('value')) #close driver session driver.quit()
Output
- Related Articles
- How to delete default values in text field using Selenium?
- How can I delete an element in Selenium using Python?
- How can I clear text of a textbox using Python Selenium WebDriver?
- How do I delete a file in Python?
- How to send keyboard input to a textbox on a webpage using Python Selenium webdriver?
- How to send a report through email using Selenium Webdriver?
- How to read a text file in Selenium with python?
- How to write a text file in Selenium with python?
- How to send cookies with selenium webdriver?
- How do I get a parent HTML Tag with Selenium WebDriver using Java?
- How to send a simple text based email using a JSP page?
- How do I give focus to a python Tkinter text widget?
- How do I pass options to the Selenium Chrome driver using Python?
- How do I automatically download files from a pop up dialog using selenium-python?
- How to create a text field using JavaFX?

Advertisements