Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send a DELETE keystroke to a text field using Selenium with Python?

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?

like image 469
Nick Sweeting Avatar asked Dec 07 '14 01:12

Nick Sweeting


People also ask

How do you press Delete in Selenium?

New Selenium IDE 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.

How do I use keyboard keys in Selenium Python?

We can send keyboard input to a textbox on a webpage in Selenium webdriver in Python using the method send_keys. The text to be entered is passed as a parameter to that method. To perform keyboard actions, we can also use the send_keys method and then pass the class Keys.

How do you clear and enter values in Selenium?

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.

How do you clear the content of a text box in Selenium?

New Selenium IDE We shall use the clear method to remove the content from a text area or an edit box. First, we shall identify the text area with the help of any locator. A text area is identified with a textarea tagname in the html code. Let us input some text inside the below text area, then clear the text.


2 Answers

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
like image 183
falsetru Avatar answered Oct 17 '22 15:10

falsetru


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)
like image 17
Hamed Baziyad Avatar answered Oct 17 '22 14:10

Hamed Baziyad