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.
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 ’
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.
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.