22

How do I send a Keys.DELETE keystroke to a text field with the Selenium web tester? I'm trying to simulate the user typing in a field and then deleting what they typed to test the interactive autosuggestion feature. It should filter the list to items beginning with their query, then show all the possible suggestions again when they delete their query. Unfortunatley sending a .clear() doesn't work to un-filter the list again. Neither does send_keys('\127').

def get_suggestions():
    driver.get('https://www.example.com/')
    driver.find_element_by_css_selector('#searchQuery').click()
    driver.find_element_by_css_selector('#searchQuery').send_keys('a')
    sleep(0.5)
    driver.find_element_by_css_selector('#searchQuery').send_keys(Keys.DELETE)
    sleep(0.5)
    for suggestion in driver.find_elements_by_css_selector('#search-form #suggestions'):
        yield suggestion

How can I simulate the user pressing the delete button on their keyboard?

Nick Sweeting
  • 5,364
  • 5
  • 27
  • 37

7 Answers7

26

You need to use Keys.BACKSPACE instead of Keys.DELETE if you want to delete a character before the cursor. Keys.DELETE is used to delete a character after the cursor.

Be sure you are using the following import:

from selenium.webdriver.common.keys import Keys
jason m
  • 6,519
  • 20
  • 69
  • 122
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Yup, ironically I just found the answer after posting this by seeing Keys.BACKSPACE in the autosuggestion list in iPython when I typed Keys. Feeling kind of dumb right now. – Nick Sweeting Dec 07 '14 at 02:04
  • 1
    @NickSweeting, Maybe you're using a Mac? The keyboard layout in the Mac make it confusing ;) – falsetru Dec 07 '14 at 02:05
21

You can use Ctr+a to highlight the text and remove it by BACKSPACE:

from selenium.webdriver.common.keys import Keys

element.send_keys(Keys.CONTROL, 'a')
element.send_keys(Keys.BACKSPACE)
Tahlor
  • 1,642
  • 1
  • 17
  • 21
Hamed Baziyad
  • 1,954
  • 5
  • 27
  • 40
8

In python if element.clear() (doesn't fire onChange in react) doesn't work try this.

def clear_text(element):
            length = len(element.get_attribute('value'))
            element.send_keys(length * Keys.BACKSPACE)
ncrmro
  • 177
  • 2
  • 12
6

Why did no one say the import required?

from selenium.webdriver.common.keys import Keys
jason m
  • 6,519
  • 20
  • 69
  • 122
4

Also, if you want to delete the entire text you can mimic the ctrl + a & backspace in a single function like below:

def clear_entire_text(element):
    element.send_keys(Keys.CONTROL + 'a', Keys.BACKSPACE)
Automation Engr
  • 444
  • 2
  • 4
  • 26
0

I'm trying to simulate the user typing in a field and then deleting what they typed

If you are trying to delete one key at a time, then, as @falsetru mentions, using the following will delete one character at a time:

element = driver.find_element_by_css_selector('#searchQuery')
element.send_keys(Keys.BACKSPACE)

However, if you want to delete the entire search query, I would rather clear the input field.

element = driver.find_element_by_css_selector('#searchQuery')
element.click()
element.clear()
Brad Ahrens
  • 4,864
  • 5
  • 36
  • 47
0

How to delete a line of text

This shortcut did not work for me Ctrl + a
But SHIFT + END worked perfectly

If the cursor is at the end of the line use SHIFT + HOME

action = ActionChains(driver)
action.key_down(Keys.SHIFT).send_keys(Keys.HOME, Keys.BACKSPACE).perform()

If the cursor is at the start of the line use SHIFT + END

action = ActionChains(driver)
action.key_down(Keys.SHIFT).send_keys(Keys.END, Keys.BACKSPACE).perform() 
Herker
  • 552
  • 6
  • 20