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);
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.
Syntax. To delete all the keys entered simultaneously, we have to pass CTRL+A and BACKSPACE as parameters to the send_keys method.
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.
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 tosendKeys(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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With