How to clear a pre-filled text field (Android)

Hi,

I’ve been sending keys to fill in a field that usually if you tap into it on the device is cleared as soon as you tap into it, but with Appium i click into the field and then .send_keys “test” and it just appends it onto the pre-filled text. How do I clear it? I’ve tried a couple of methods but so far nothing works.

Grateful for any help

You can try using the .clear method like:

element.clear

as in

tag("android.widget.EditText").clear

On android it tends to raise an exception although it clears the text, so you need to handle that.

You alternatively can do a method with

press_keycode 67 #delete key 

for example you collect the current text size on your element and you send that amount of delete keys with the element previously selected.

Just play around :slight_smile:

1 Like

Well.clear just caused the error without clearing the text 'info: [debug] Responding to client with error: {“status”:13,“value”:{“message”:“An unknown server-side error occurred while processing the command.”,“origValue”:“Clear text not successful.”},“sessionId”:“d173ba2b-ae41-45cf-8c24-4049e5e77363”}
info: ← POST /wd/hub/session/d173ba2b-ae41-45cf-8c24-4049e5e77363/element/9/clear 500 12002.831 ms - 195 ’

Off to try the other method!

Well that didn’t work either - and if i attempt to just directly fill the field in without clearing

Finding methods on class: class com.android.uiautomator.core.UiAutomatorBridge
info: [debug] [BOOTSTRAP] [debug] Clearing text not successful falling back to UiAutomator method clear
info: [debug] [BOOTSTRAP] [debug] clearText not successful, continuing with setText anyway
info: [debug] [BOOTSTRAP] [debug] Sending plain text to element: Please enter your surnametest

The ‘Please enter your surname’ is the pre-filled text that’s meant to be cleared but it’s almost like it’s picking it up as an input somehow.

Scratches head.

Post your code please

#wait { find_element(:xpath, “//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.ViewSwitcher[1]/android.widget.FrameLayout[1]/android.widget.ScrollView[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[2]/android.widget.EditText[1]”).click }
wait { find_element(:xpath, “//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.ViewSwitcher[1]/android.widget.FrameLayout[1]/android.widget.ScrollView[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[2]/android.widget.EditText[1]”).send_keys “test” }

Actually - i think the press_keycode 67 method will work, thanks Telmo.

Nope - looks like as soon as the text is deleted is goes back to being pre-populated,
this the error i’m seeing

info: [debug] [BOOTSTRAP] [debug] Clearing text not successful using selectAllDelete now trying to send delete keys.

info: [debug] [BOOTSTRAP] [debug] Finding methods on class: class com.android.uiautomator.core.UiAutomatorBridge

info: [debug] [BOOTSTRAP] [debug] Clearing text not successful falling back to UiAutomator method clear

info: [debug] [BOOTSTRAP] [debug] clearText not successful, continuing with setText anyway

info: [debug] [BOOTSTRAP] [debug] Sending plain text to element: Please enter your surnametest

ok - so this suggests it’s a known issue: https://github.com/appium/appium/issues/3543 which was fixed in 1.2.2 - off to give that a try!

fixed in version 1.2.3 not 1.2.2

This is happening with me even when running from the source :frowning:

The same exception as you mentioned here:

try upgrading to to 1.3.1 - also be aware that you might have to stick a couple of empty spaces after entering text.

I am running from source so it should have the latest changes…

from master - yup guess it should have those fixes, very strange. I upgraded using the dmg and that got rid of the error for me.

I think this is a bug and we need to report it on Github to be fixed ??

I think it was mentioned here on Github for 1.2.2 - maybe update? https://github.com/appium/appium/issues/3543

I updated it with the error details. Hopefully they open and fix it soon :frowning:

Just to update , clear fails in case text field has multi strings.

It will only work for single string text input.

One case use type method if text field has multiple strings.

1 Like

I have created a method and this works fine…although exploring more on the possibilities…

public void clear(String locatorType, String locator, long… waitSeconds)

{

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

String text = we.getText();

int maxChars = text.length();

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

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

}

1 Like

That appears to be a simpler solution than the one I came up with. I had tried yours, but I found that

a) I couldn’t be guaranteed that when I made that text the focus, I was guaranteed that the cursor was at the end of the text
b) I haven’t found a way to determine the location with the string where the cursor is focused. If I just send delete (67) text.length times, it may not delete the entire string.

Here is my solution

def clear_text_field(field)
  begin
    field.clear
  rescue Selenium::WebDriver::Error::UnknownError
    clear_field_forcefully field
  end

# Manually delete each character from the text field
#
# @param <object> element field from Appium
def clear_field_forcefully(field)
  # Get into the text field
  field.click
  # Move left one spot to avoid having "grayed out" text count as actual
  # text when the field is empty IFF we clicked at the end of the field
  # and only have text to the left to delete
  appium_driver.driver.press_keycode KEYCODES[:dpad_left]

  current_text = field.text.length
  # Delete characters to the left until we are at the start
  begin
    prev_text = current_text
    appium_driver.driver.press_keycode KEYCODES[:delete]
    current_text = field.text
  end until (current_text == prev_text)

  # Delete all characters to the right
  len = current_text.length
  1.upto(len) do
    appium_driver.driver.press_keycode KEYCODES[:dpad_right]
    appium_driver.driver.press_keycode KEYCODES[:delete]
  end
end

It’s not my favorite solution, but it’s handled all devices we’ve tested so far.