sramise
sramise

Reputation: 11

In java selenium i am trying to send backspace character ('\uE003') or (\u0008) to a textbox to clear existing text

textboxElement.sendKeys("\uE003");
(or)
textboxElement.sendKeys("\u0008");

But instead of removing the existing text in the textbox, 003 or 0008 is getting added to the textbox. i cant use Keys.BACK_SPACE because it is not working on Ubuntu server.

Upvotes: 0

Views: 1892

Answers (2)

Mike ASP
Mike ASP

Reputation: 2333

(1) first import selenium Keys to your class

(2) best way to clean/clear any input field through using length ,below method will delete/backspace the all character in dynamic manner:

public void clearText(WebDriver driver, WebElement element)
{
        String areaText = element.getText();
        int  lengthOfString = text.length();

        for(int i = 0 ; i < lengthOfString ; i++)
        {
            element.send_keys(Keys.BACKSPACE);
        }
}

or we can use below:

element.sendKeys(Keys.CONTROL + "a");
element.sendKeys(Keys.DELETE);

or 

element.send_keys(Keys.CONTROL, 'a')
element.send_keys(Keys.BACKSPACE)

Upvotes: 0

d_air
d_air

Reputation: 641

Try applying the usual control a, then delete. This will clear the whole text in a textfield.

textboxElement.sendKeys(Keys.CONTROL + "a");
textboxElement.sendKeys(Keys.DELETE);

Upvotes: 1

Related Questions