Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clear text field using DELETE or BACK SPACE key in webdriver

I am trying to clear a text field using this action:

emailField.sendKeys("gmail.com"); emailField.sendKeys(Keys.CONTROL,"a",Keys.DELETE); 

In above code, the last line only selects the text, does not delete it, but if I separate the actions it works.

emailField.sendKeys(Keys.CONTROL,"a"); emailField.sendKeys(Keys.DELETE); 
like image 768
amit dobriyal Avatar asked Mar 06 '16 19:03

amit dobriyal


People also ask

How do you use backspace keys?

The Backspace key has one function: delete text to the left of the cursor. In some Internet browsers, you can also press the Backspace key to go back to the previous page. However, most browsers today use the shortcut combination Alt+left arrow key instead.

How do you select all and delete in selenium?

Syntax. To delete all the keys entered simultaneously, we have to pass CTRL+A and BACKSPACE as parameters to the send_keys method.

What is selenium key?

The selenium has n number of keys that will be used in the different areas of automation testing to perform the user operations. The keys provided with different types like enum, string, integer etc these are some default keys which provided from the user end and other web elements, drivers will support the datatypes.


1 Answers

From the JavaDoc for WebElement.clear():

If this element is a text entry element, this will clear the value. Has no effect on other elements. Text entry elements are INPUT and TEXTAREA elements. Note that the events fired by this event may not be as you'd expect. In particular, we don't fire any keyboard or mouse events. If you want to ensure keyboard events are fired, consider using something like sendKeys(CharSequence) with the backspace key. To ensure you get a change event, consider following with a call to sendKeys(CharSequence) with the tab key.

Most likely you simply need to call:

emailField.sendKeys("gmail.com"); emailField.clear(); 

But if you need the clearing to be done via the keyboard for some reason, use Keys.BACKSPACE.

like image 192
Andrew Regan Avatar answered Oct 07 '22 13:10

Andrew Regan