1
votes

Is there a way i can select complete text in a edit field and delete. I am trying to automate mobile app using Selenium Appium and Java and there is a field which has 10-15 characters. I want to delete the existing content in the field and then update it with .sendkeys() command.

driver.findElement(By.xpath("elmentpath")).clear(); -- does not work also driver.findElement(By.xpath("elmentpath")).click(); -- this command clicks on center of the string after which if i run driver.sendKeyEvent(AndroidKeyCode.BACKSPACE); --- it clears only half of the string so if there is way to click at the end of string i can run driver.sendKeyEvent(AndroidKeyCode.BACKSPACE); -- command in a loop with string length. PLease help

6
There are some pretty good answers that you can find here: stackoverflow.com/questions/22679960/appium-clear-a-field - Robert Brown
After you click into the field, can you send CTRL+A to select all, and then backspace? - JeffC

6 Answers

0
votes

You can do element.clear() or element.sendKeys(""). Though 2nd one is not recommended but I had to use it when my development box's Display text size was set to Medium or Larger.

0
votes

I think you can just set up an instance of the selenium Actions class to support CTRL+A and delete.

WebElement textWebElement = webDriver.findElement(By.xpath("elmentpath"));
Actions actions = new Actions(webDriver);
actions.sendKeys(textWebElement, Keys.chord(Keys.CONTROL, "a").perform();
actions.sendKeys(textWebElement, Keys.DELETE).perform();

I'm not certain how this works with the Android API, but that's how I typically handle it in my junit content.

Please note that the CTRL+A event is sent as a chord, not as separate events, or as a character array.

Good Luck!

0
votes

Have you tried double clicking on the webelement allowing entire text to be highlighted and press one backspace to clear the entire text.

TouchAction action = new TouchAction(driver).tap(element).waitAction(800).tap(element);
action.perform();
driver.sendKeyEvent(AndroidKeyCode.BACKSPACE); 
0
votes

If you are trying to automate android application then you can directly take the id of the text-field and pass the values. It will clear the contents.

WebElement textfield = webDriver.findElement(By.id("text-field ID"));

textfield.sendKeys("values");
0
votes

There was a problem with Appium older version. Try using the latest version of Appium. I am using 1.4.16.1, and my I never faced such issue after that.

Refer the link for more details - https://github.com/appium/appium/issues/4565

Let me know if you still face issue after having newer version with you.! Thanks.

0
votes
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();

Working for iOS 14 with Appium 1.21.0