13

I have my login screen in app now. Each time the app is launched in screen the mobile number is pre filled with the older text.

I just want to know I have tried:

WebElement mob = driver.findElement(By.name("Mobile Number"));
mob.clear // Not working

I have tried :

String Mobile
mob="";   

but still it cannot delete the pre filled text.

I am trying to automate my android app using appium, please help me with this.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user1664899
  • 327
  • 2
  • 4
  • 15
  • 1
    Do mark an answer to the question. It helps other know of whats already existing and how could that be worked out. – Naman Sep 16 '17 at 11:58

27 Answers27

7

A click on to the textBox before you clear should do with the latest libs:

WebElement mob = driver.findElement(By.name("Mobile Number"));
mob.click();
mob.clear();

OR from the sample jUnitTest here

WebElement text = driver.findElement(By.xpath("//UIATextField[1]"));
text.sendKeys("12");
text.clear();

I guess

Naman
  • 27,789
  • 26
  • 218
  • 353
6

I had trouble with this too. A main issue I found was that in order to delete the area by pressing the delete key was that it needed to tap at the end of the line. This works for me:

public void clearTextField(WebElement element) {
    double x = element.getLocation().getX() + element.getSize().width - 5;
    double y = element.getLocation().getY() + ((double) element.getSize().height / 3);
    preciseTap(x, y, 0.1, 1);
    while (!element.getText().isEmpty()) {
        pressDeleteKey();
    }
}

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

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

It is a lot slower than just clearing it all out but I have not figured out how to do this yet. Would be ideal to double tap or tap and hold until everything is selected.

plosco
  • 891
  • 1
  • 10
  • 18
6

It is definitely not efficient, could be improved and there's probably a better way... But, using adb's shell input key event codes I simply called "dpad right" to move the cursor all the way to the right. Once there, send the key code "DEL" to start deleting all the way back... So... two for-loops. This was mainly used for short texts:

public void cleatTextFully(WebElement element) {
    int stringLength = element.getText().length();

    for (int i = 0; i < stringLength; i++) {
        mDriver.sendKeyEvent(22); // "KEYCODE_DPAD_RIGHT"
    }

    for (int i = 0; i < stringLength; i++) {
        mDriver.sendKeyEvent(67); // "KEYCODE_DEL"
    }
}

mDriver is the AppiumDriver instance. Hope this helps some what.

Alon Minski
  • 1,571
  • 2
  • 19
  • 32
  • 1
    if edit text contains hint text then I am not sure your code Will work or not.. Once I tried same approach, after clearing text box also length was equal to the hint text. – Chandrashekhar Swami Feb 07 '16 at 04:26
  • This is the method which works for me with Ionic. But I only have to use KEYCODE_DEL, because a click into the textfield already puts the cursor to the right. – Highriser Apr 05 '17 at 07:52
3

We can use selectAll if tests are executing on android.

  • longPress on textfield
  • Locate and tap on element of selectAll (Android select all button)
  • sendKeyEvent(67)

It will select entire text and will press delete.

Kevin Shah
  • 41
  • 3
  • Note: in Android Virtual Devices emulator, the emulator will helpfully deselect any select-all long-press on password fields. – kiminoa Oct 16 '15 at 20:31
3

I was able to resolve this issue using the code below as "textField().clear()" didn't appear to work on some devices and emulators such as Genymotion.

while (!textField().getText().isEmpty()) {
   TouchAction touchAction = new TouchAction(driver);
   touchAction.longPress(textField());
   driver.getKeyboard().sendKeys(Keys.DELETE);
}

The code would loop through until textfield is cleared. Hope this helps, works for both Android and iOS.

2

you might want to refrain from using appium's built in clear method.

you can try using adb shell input keyevent 67 which sends a delete keyevent to android. this works on all versions pretty much.

you can just click the textfield twice to highlight the text:

WebElement mob = driver.findElement(By.tagName("editText"));

that would locate the first textField avilable, as other methods in selenium bindings, you can try findElements as well, and get all the textfields in an array, and you can choose whichever you meant to manipulate.

then, send an input keyevent 67 to delete the whole field.

Dnile
  • 21
  • 3
1

If the text field contains any pre-specified mobile number, please do in the following way.

WebElement mob = driver.findElement(By.name("xxxxxxxxxx")); mob.clear();

xxxxxxxxxx: mobile number that is pre-specified while opening the application.

Else use some other locating techniques like By.xpath , By.id(if you are testing android and Selendroid as capability) etc.

Abhishek Swain
  • 1,054
  • 8
  • 28
  • as mentioned above i have tried .clear in both ways. – user1664899 Mar 31 '14 at 06:52
  • 1
    .clear() does not work for native apps like it would on the web. I have also tried to do a double tap but it is too slow since touchCount doesnt also work. – plosco Apr 21 '14 at 19:28
1

This method worked for me, although I am trying for a better solution. In the meantime please use this method.

public void clear(String locatorType, String locator, long... waitSeconds)
{

WebElement we =  getElementWhenPresent(getByLocator(locatorType, locator), waitSeconds);

String text = we.getText();

int maxChars = text.length();

//we.clear() is not working

for (int i = 0; i < maxChars; i++)

((AppiumDriver)driver).sendKeyEvent(67);

}
Hamed Ali Khan
  • 1,108
  • 3
  • 23
  • 29
abhii2628
  • 39
  • 1
  • 7
1

To avoid recent iOS 8.x errors I use the following (where by is an instance of By representing your textfield/textview):

    int stringLength = driver.findElement(by).getText().length();
    if (stringLength > 0) {
        driver.findElement(by).clear();
    }

If you try and call clear() against an already clear textfield I was tending to get errors in appium.

Charlie S
  • 4,366
  • 6
  • 59
  • 97
1

I faced similar issue recently. What I did, get the text from edit text if equals "", then send keys else called clear() method.

Naman
  • 27,789
  • 26
  • 218
  • 353
Chandrashekhar Swami
  • 1,742
  • 23
  • 39
0

This normally happens when the opened screen in your app, is a "WebView".

When faced with similar problem, I followed the below steps:

  1. Got the value of the textfield, i.e., prefilled text, using the "getAttribute" method. And, then retrieved the length of the string, so as to know how many characters are present to delete

  2. Tapped on the textfield.

  3. Deleted the characters inside the textfield using the "Delete button" element of the iOS Keyboard.

  4. Filled the necessary value in the textfield.

Below, is the code that might just help you out in resolving your problem:

WebElement ele = driver.findElement(By.xpath("//Locator of the textfield")); //Locating the textfield

int characters_to_delete = ele.getAttribute("value").length();//Counts the number of characters to delete

ele.click(); //Tapping or Clicking on the textfield

// Deleting or Clearing the textfield off of the prefilled values
for(int i=0;i<characters_to_delete;i++){
 driver.findElement(By.xpath("//Locator of the Keyboard button 'Delete' ")).click();
}

ele.sendKeys("value to be filled in the textfield"); //Filling the textfield with the value
Subh
  • 4,354
  • 1
  • 13
  • 32
0
driver.findElement(By.name("Mobile Number")).clear();

this works for me. I am using find element by id and then calling clear.

wd.findElement(By.id("username")).clear();

This clears the pre-entered data in the field user name in my case.

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
Suman
  • 436
  • 4
  • 10
0

I faced the same issue.

Tried to click, clear and send keys, it worked.

See this code

driver.findElement(By.name("address").click(); 
driver.findElement(By.name("address").clear();
driver.findElement(By.name("address").sendKeys("something");`
Farhan
  • 2,535
  • 4
  • 32
  • 54
0

It's about login screen so i would suggest to restart the app each time you run your script, go to appium and check Full Reset in Android Settings.

Emna Ayadi
  • 2,430
  • 8
  • 37
  • 77
0

For iOS, I can perform backspace/delete by using below code of snippet:

    //Get the keyword
    String name = driver.findElement(by).getText();
    WebElement e1 = driver.findElement(by);
    System.out.println("Length of Keyword = " +name.length());
    int count = 0;
    int keywordlength = name.length();
    //click on keyword textbox
    e1.click();
    while (count < keywordlength) {
        //Clear the keyword
           TouchAction ta = new TouchAction(driver);
           ta.longPress(e1);
           driver.getKeyboard().sendKeys(Keys.DELETE);
           count++;
    }
0

This thing can be overcome by using the latest version of appium. In older version clear sometimes doesn't clear all the text-fields in case of longer text.

thisisdude
  • 543
  • 1
  • 7
  • 31
  • Are you referring to *clear text fix for large centered edit fields* version [1.3.4](https://github.com/appium/appium/blob/master/CHANGELOG.md#user-content-android-12)? – Jon Jan 31 '17 at 15:25
0
    WebElement element2 = appiumDriver.findElement(By.xpath(element));
        element2.sendKeys(Keys.CONTROL + "a");
         element2.sendKeys(Keys.DELETE);
  • 4
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – J. Chomel Feb 14 '17 at 10:22
  • 1
    will find the element and than select all and delete the selected content – akhilesh gulati Feb 14 '17 at 11:05
0
List<WebElement> x=driver.findElements(By.className("enter widget details")); //You can get widget details via android UIautomator
driver.findElementById("enter widget details").click(); //You can get widget details via android UIautomator
x.get(index).clear();
x.get(index).clear();

Please note this worked for me but you may have to maneuver your code as per your requirements, thanks.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Star
  • 73
  • 7
0
def Clear(self):
    self.driver.keyevent(123)#Move Cursor Last one
    time.sleep(2)
    for i in range(10):
        self.driver.keyevent(67)#Deleted Keyboard 
        self.driver.implicitly_wait(60)
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
  • Welcome to Stack Overflow! Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Apr 24 '17 at 11:44
0

What helped for me was to add an empty sendKeys before the clear.

Like this:

public void sendKeysToElement(WebElement element, String keys) {
    element.sendKeys("");
    element.clear();
    element.sendKeys(keys);
    driver.hideKeyboard();
}

This method is used for all our input fields. Without the empty sendKeys it was not clearing all the fields properly.

HWentzke
  • 11
  • 6
0

I use cucumber and ruby to write my tests and this one works good for both iOS and Android for me.

arg1 and arg2 are the input values that I give in the gherkin steps.

And(/^I enter "([^"]*)" on identifier "([^"]*)"$/) do |arg1, arg2|
      Input = find_element(id: arg2)
      Input.click
      Input.clear
      Input.send_keys(arg1)
    end
Emjey
  • 2,038
  • 3
  • 18
  • 33
  • Thank you for this code snippet, which may provide some immediate help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its educational value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight May 23 '17 at 13:59
  • @TobySpeight Edited accordingly! – Emjey May 24 '17 at 06:19
0

I have faced the same issue, but i found one proper solution.

 public void clearTextBox(WebElement element) throws Exception
  {
      element.click();
      element.sendKeys(Keys.CONTROL + "a");
      element.sendKeys(Keys.DELETE);          
  }

It will work for sure.

0

Is working for me:

action.longPress(WebElement).perform();
WebElement.clear();
  • Ok @DieterMeemken, for the context: `import io.appium.java_client.TouchAction; ` `WebElement mob = driver.findElement(By.name("Mobile Number"));` `TouchAction action = new TouchAction(driver);` `action.longPress(mob).perform();` `mob.clear();` Hope that it's clear ? – Adama Diagne Sep 06 '18 at 13:48
0

I am using a double tab and send an empty key to the field. This works stable in my UI tests.

new TouchAction<>((AndroidDriver<?>) driver).tap(new TapOptions().withTapsCount(2).withElement(new ElementOption().withElement(element))).perform();
element.sendKeys("");
AndiCover
  • 1,724
  • 3
  • 17
  • 38
0

I HAD A HARD TIME IN SOLVING THIS ISSUE AS WHILE AUTOMATING ios APP was unable to clear a specific field

was not able to use any other alternatives as well so this worked out in my case!

1.Get the XPath of delete button of keyboard and then loop it and use click method to clear it this is what worked in my case

for(int x=1;x<6;x++)
{
    driver.findElementByXpath("//XCUIElementTypeKey[@name='Delete']").click;
}
Nino
  • 6,931
  • 2
  • 27
  • 42
ANMOL
  • 1
0

The verified solution for clearing the text in IOS Simulator or device

MobileElement element = driver.findElement(By.id("id_data"));
Rectangle rectangle = element.getRect();
int YAxis = element.getCenter().getY();
int XAxis = rectangle.getWidth();
TouchAction action = new TouchAction(this.getIOSDriver());
action.tap(TapOptions.tapOptions().withPosition(PointOption.point(XAxis, 
YAxis))).perform();
element.clear();

Verified on Simulator iOS 14.3 with appium 1.21.0

-1

I'm working with python and this is what I use:

 self.driver.find_element_by_id(element_id).click()
 self.driver.find_element_by_id(element_id).send_keys('')

I hope that it helps.