TestProject Forum

General => Appium => Topic started by: Sebastian on March 08, 2016, 19:45:04 pm

Title: Clear text field using appium
Post by: Sebastian on March 08, 2016, 19:45:04 pm
On the main screen of my app one of my text fields is already populated with text from previous run.

I tried running :

WebElement nameField = driver.findElement(By.name("Name"));
nameField = " "

But both options are not working.

The text won't go away.

Any help would be appreciated
Title: Re: Clear text field using appium
Post by: Oren on March 09, 2016, 11:07:30 am
I also struggled with this issue. One thing i found was that if I wanted to delete text area by pressing the delete key, it needed to tap at the end of the line.

public void clearText(WebElement webElement) {
    double xLocation = webElement.getLocation().getX() + webElement.getSize().width - 5;
    double yLocation = webElement.getLocation().getY() + ((double) webElement.getSize().height / 3);
    tap(xLocation, yLocation, 0.1, 1);
    while (!webElement.getText().isEmpty()) {

public void pressDelete() {
    HashMap swipeObject = new HashMap();
    swipeObject.put("keycode", 67);
    ((JavascriptExecutor) driver).executeScript("mobile: keyevent", swipeObject);

public void tap(double xLocation, double y, double duration, int touchCount) {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    HashMap<String, Double> tap = new HashMap<String, Double>();
    tap.put("x", xLocation);
    tap.put("y", yLocation);
    tap.put("touchCount", (double)touchCount);
    tap.put("duration", duration);
    js.executeScript("mobile: tap", tap);

Hope this helps.