clear text field using DELETE or BACK SPACE key in webdriver
Asked Answered
D

10

30

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);
Downandout answered 6/3, 2016 at 19:0 Comment(0)
V
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.

Vanburen answered 6/3, 2016 at 23:43 Comment(1)
The backspace key is written as "\b" (from #6448757)Inpour
D
26

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

emailField.sendKeys(Keys.BACKSPACE)
Description answered 11/10, 2017 at 9:40 Comment(0)
G
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));
Graber answered 19/4, 2018 at 19:43 Comment(1)
tks! ->sendKeys(array(WebDriverKeys::CONTROL,'a',WebDriverKeys::DELETE))->sendKeys(array($title)Shepherd
C
6

Tested in chrome driver

WE.send_keys(' \b')

This will add space then delete it (backspace)

Cleliaclellan answered 15/8, 2018 at 0:14 Comment(0)
K
2

I use in javascript and it's working fine:

await textBox.sendKeys(value);
await textBox.sendKeys(Key.BACK_SPACE);
Knowles answered 17/4, 2020 at 5:50 Comment(0)
H
1

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
Hollishollister answered 30/12, 2022 at 2:26 Comment(0)
O
0
emailField.sendKeys(Keys.BACKSPACE)

doesn't worked for me .

I used 'Key' instead of 'Keys'

emailField.sendKeys(protractor.Key.BACKSPACE)
Ohmmeter answered 1/11, 2018 at 16:12 Comment(2)
What is the package of the Key class that you are usingPyrex
I have used protractor.Key.BACKSPACEOhmmeter
A
0

emailField.sendKeys(Keys.CONTROL + "a",Keys.DELETE);
Augmentative answered 14/4, 2022 at 11:29 Comment(0)
C
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']);

Corley answered 30/10, 2022 at 7:2 Comment(0)
F
-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=> [email protected]

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.

Fuchsia answered 31/7, 2019 at 2:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.