1

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

Anuj Shrivastav
  • 393
  • 2
  • 8
  • 18
  • There are some pretty good answers that you can find here: http://stackoverflow.com/questions/22679960/appium-clear-a-field – Robert Brown Oct 14 '15 at 21:42
  • After you click into the field, can you send CTRL+A to select all, and then backspace? – JeffC Oct 14 '15 at 22:10

7 Answers7

1

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!

Jeremiah
  • 1,145
  • 6
  • 8
0

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.

JsingH
  • 189
  • 7
0

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); 
debugger89
  • 2,108
  • 2
  • 14
  • 16
0

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");
Vignesh
  • 165
  • 4
  • 15
0

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.

thisisdude
  • 543
  • 1
  • 7
  • 31
0
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

0

I had the same issue, and using WebElemnt.clear() does not work properly, so in order to try to simulate exactly the humain intervention, I used:

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;
    
    // I am using AndroidDriver for Android testing (new AndroidDriver<AndroidElement>(nodeUrl, capabilities)) 
    AndroidDriver<AndroidElement> androidDriver = (AndroidDriver<AndroidElement>)driver;
    String presentText = webElement.getText();;
    if(presentText != null) {
        // Put the cursor on the desired testfield
        webElement.sendKeys("");
        // Press delete button as many times as the existing text length
        for (int i = 0; i < presentText.length(); i++) {                
            androidDriver.pressKey(new KeyEvent(AndroidKey.DEL));
        }
    }

This piece of code did exactly what I was expecting

Shessuky
  • 1,846
  • 21
  • 24