Tap on end of text in a textbox

I am trying to delete a text in the textbox.

Currently, I can do that using the command

driver.findElement(By.xpath("//*[@resource-id=‘textbox’]")).click();
driver.getKeyboard().sendKeys(Keys.DELETE);

but if the text is centered or long enough to reach the center of the field i am unable to delete the whole text. Is there any way that i can tap the textbox at the end of its existing text?

Hi,

Have you tried Appiums inbuilt method clear()?

driver.findElement(By.xpath("YOUR_XPATH")).clear();

In some cases clear() may not always work, native apps may react differently.

Another possible solution I see is the following:

WebElement element = driver.findElement(By.xpath("YOUR_XPATH")).click();
element.click();
element.sendKeys(Keys.CONTROL + "a");
elemnet.sendKeys(Keys.DELETE);

Another possible solution that could work is to perform the click twice to highlight the entire text and then performing your Keys.DELETE action.

If none of the solutions work, could you provide more information in regards to if you are using a Native / Hybrid app and in what context it is being executed.

I’ve seen a similar issue before, I would also suggest to look through this.

Thanks for your answer. Yes, that would work in a web page. I forgot to mention that I am trying to do it in iOS and Android app. The text is centered on the textboxes so I am unable to delete all the texts.

Try the TouchAction Class:

This example is double tapping, if it selects the entire text then you could perform a delete action.

TouchAction touchAction = new TouchAction(driver).tap(element).waitAction(800).tap(element);
touchAction.perform();

Another solution that should work is:

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

I am thinking, if you wish to tap at the end of textbox you probably need to find the coordinates programmatically, or do something complicated. This discussion seems to suggest multiple ways, hope it helps!