29

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);
Chachmu
  • 7,725
  • 6
  • 30
  • 35
amit dobriyal
  • 391
  • 1
  • 3
  • 4

10 Answers10

29

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.

Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
  • 9
    The backspace key is written as "\b" (from http://stackoverflow.com/questions/6448257/selenium-delete-contents-from-a-textbox/22603085#22603085) – Lucy Bain Dec 28 '16 at 05:19
25

keys.DELETE can not work to delete the input text,you should use keys.BACKSPACE.

emailField.sendKeys(Keys.BACKSPACE)
LPhuang
  • 251
  • 3
  • 4
8

From the JavaDoc for Keys.chord

chord(java.lang.CharSequence... value) Simulate pressing many keys at once in a "chord".

You should be able to use

emailField.sendKeys(Keys.chord(Keys.CONTROL,"a",Keys.DELETE));
Tabatha
  • 103
  • 2
  • 7
6

Tested in chrome driver

WE.send_keys(' \b')

This will add space then delete it (backspace)

Bassem Shahin
  • 656
  • 7
  • 13
2

I use in javascript and it's working fine:

await textBox.sendKeys(value);
await textBox.sendKeys(Key.BACK_SPACE);
0
emailField.sendKeys(Keys.BACKSPACE)

doesn't worked for me .

I used 'Key' instead of 'Keys'

emailField.sendKeys(protractor.Key.BACKSPACE)
Gabson
  • 55
  • 12
RRR
  • 75
  • 2
  • 11
0

emailField.sendKeys(Keys.CONTROL + "a",Keys.DELETE);
0

In PHP:

if you use php-webdriver (https://github.com/php-webdriver/php-webdriver) you must:

    use Facebook\WebDriver\WebDriverKeys AS Keys;
    .
    .
    .
    $this->driver->findElement(By::id('demo'))->sendKeys([Keys::BACKSPACE,'Any other text']);

0

Just adding another working C# example using the Google Chrome webdriver.

SendKeys only takes one parameter so created a string with the Crtl + A. This code sequence will select the current text in the field then delete the text.

Code example:

var crtlA = Keys.Control + "a";

driver.FindElement(By.XPath("//div[3]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div/span/input")).SendKeys(crtlA); Wait(5000); // Select current text
driver.FindElement(By.XPath("//div[3]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div/span/input")).SendKeys(Keys.Delete); Wait(5000); // Clear current text
driver.FindElement(By.XPath("//div[3]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div/span/input")).SendKeys(newItemSku); Wait(5000); // Input SKU name
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
NarGuy
  • 1
  • 1
-1
1. in WebdriverIO, i tried to edit the text by clear text (which contains special charactes like @, +, _) in text field by below following step. Eventhough it was not successful.

example: text=> abc+1234@gmail.com

step1:browser.clearElement(selector);

step2:browser.execute(function () {
            document.querySelector(>>>Cssselector<<<).value="";
        });

step3: browser.doubleClick(selector);
       browser.keys("Delete");

step4: browser.click(selector);
       browser.keys(['Meta',a]);  
       browser.keys('Meta');   
       browser.keys('Delete');

Note: below step is resolved this issue.

var count= browser.getAttribute(selector, value).length;
for (var i=0;i<count;i++)
{
if (browser.getAttribute(selector, value)=='')
break;
}
else
{
browser.doubleClick(selector);
browser.keys("Delete");
}
browser.pause(200);

// it will clear your text field easily.

Note: You can add the new text now.