Send_keys throws StaleObjectException using uiautomator2

I’m switching from uiautomator to uiatuomator2 and I get StaleObjectException error when I try to send_keys to a text field in the UI. In my pry session, appium finds the element and clicks on it but throws the error and hangs after send_keys command. I have other text fields with similar attributes that I’m able to type into using send_keys.

If I manually type something in the text field and then use send_keys it works but not when it is empty. I have tried using different selectors, using sleep, click before send_keys with no difference. If I switch back to old uiautomator it works. get_source prints the element I’m trying to send_keys to. Manually typing a few characters in the text field does something that makes appium successfully execute send_keys.

I’m using cucumber, ruby, uiautomator2, running the test on Android 9.

Appium log shows element is present and clickable. Why does it say I’m trying to access a stale element? Please help.

[HTTP]
[HTTP] --> POST /wd/hub/session/012c27b7-cf35-46ca-bb7b-8bcc791d4b56/element
[HTTP] {"using":"id","value":"credit_card_number"}
[W3C (012c27b7)] Calling AppiumDriver.findElement() with args: ["id","credit_card_number","012c27b7-cf35-46ca-bb7b-8bcc791d4b56"]
[BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[BaseDriver] Waiting up to 3000 ms for condition
[WD Proxy] Matched '/element' to command name 'findElement'
[WD Proxy] Proxying [POST /element] to [POST http://localhost:8200/wd/hub/session/1a33069b-573c-4cd9-abf0-e72c9195c548/element] with body: {"strategy":"id","selector":"credit_card_number","context":"","multiple":false}
[WD Proxy] Got response with status 200: {"sessionId":"1a33069b-573c-4cd9-abf0-e72c9195c548","status":0,"value":{"ELEMENT":"5a726c46-c5de-4bc7-b57b-67d2691dd159"}}
[W3C (012c27b7)] Responding to client with driver.findElement() result: {"element-6066-11e4-a52e-4f735466cecf":"5a726c46-c5de-4bc7-b57b-67d2691dd159","ELEMENT":"5a726c46-c5de-4bc7-b57b-67d2691dd159"}
[HTTP] <-- POST /wd/hub/session/012c27b7-cf35-46ca-bb7b-8bcc791d4b56/element 200 37 ms - 137
[HTTP]
[HTTP] --> POST /wd/hub/session/012c27b7-cf35-46ca-bb7b-8bcc791d4b56/element/5a726c46-c5de-4bc7-b57b-67d2691dd159/click
[HTTP] {}
[W3C (012c27b7)] Calling AppiumDriver.click() with args: ["5a726c46-c5de-4bc7-b57b-67d2691dd159","012c27b7-cf35-46ca-bb7b-8bcc791d4b56"]
[WD Proxy] Matched '/element/5a726c46-c5de-4bc7-b57b-67d2691dd159/click' to command name 'click'
[WD Proxy] Proxying [POST /element/5a726c46-c5de-4bc7-b57b-67d2691dd159/click] to [POST http://localhost:8200/wd/hub/session/1a33069b-573c-4cd9-abf0-e72c9195c548/element/5a726c46-c5de-4bc7-b57b-67d2691dd159/click] with body: {"element":"5a726c46-c5de-4bc7-b57b-67d2691dd159"}
[WD Proxy] Got response with status 200: {"sessionId":"1a33069b-573c-4cd9-abf0-e72c9195c548","status":0,"value":true}
[W3C (012c27b7)] Responding to client with driver.click() result: true
[HTTP] <-- POST /wd/hub/session/012c27b7-cf35-46ca-bb7b-8bcc791d4b56/element/5a726c46-c5de-4bc7-b57b-67d2691dd159/click 200 42 ms - 14
[HTTP]
[HTTP] --> POST /wd/hub/session/012c27b7-cf35-46ca-bb7b-8bcc791d4b56/element
[HTTP] {"using":"id","value":"credit_card_number"}
[W3C (012c27b7)] Calling AppiumDriver.findElement() with args: ["id","credit_card_number","012c27b7-cf35-46ca-bb7b-8bcc791d4b56"]
[BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[BaseDriver] Waiting up to 3000 ms for condition
[WD Proxy] Matched '/element' to command name 'findElement'
[WD Proxy] Proxying [POST /element] to [POST http://localhost:8200/wd/hub/session/1a33069b-573c-4cd9-abf0-e72c9195c548/element] with body: {"strategy":"id","selector":"credit_card_number","context":"","multiple":false}
[WD Proxy] Got response with status 200: {"sessionId":"1a33069b-573c-4cd9-abf0-e72c9195c548","status":0,"value":{"ELEMENT":"720d3997-7d91-49b7-8026-7a4f827a568f"}}
[W3C (012c27b7)] Responding to client with driver.findElement() result: {"element-6066-11e4-a52e-4f735466cecf":"720d3997-7d91-49b7-8026-7a4f827a568f","ELEMENT":"720d3997-7d91-49b7-8026-7a4f827a568f"}
[HTTP] <-- POST /wd/hub/session/012c27b7-cf35-46ca-bb7b-8bcc791d4b56/element 200 34 ms - 137
[HTTP]
[HTTP] --> POST /wd/hub/session/012c27b7-cf35-46ca-bb7b-8bcc791d4b56/element/720d3997-7d91-49b7-8026-7a4f827a568f/value
[HTTP] {"value":["4"],"text":"4"}
[W3C (012c27b7)] Calling AppiumDriver.setValue() with args: [["4"],"720d3997-7d91-49b7-8026-7a4f827a568f","012c27b7-cf35-46ca-bb7b-8bcc791d4b56"]
[WD Proxy] Matched '/element/720d3997-7d91-49b7-8026-7a4f827a568f/value' to command name 'setValue'
[Protocol Converter] Added 'value' property ["4"] to 'setValue' request body
[WD Proxy] Proxying [POST /element/720d3997-7d91-49b7-8026-7a4f827a568f/value] to [POST http://localhost:8200/wd/hub/session/1a33069b-573c-4cd9-abf0-e72c9195c548/element/720d3997-7d91-49b7-8026-7a4f827a568f/value] with body: {"elementId":"720d3997-7d91-49b7-8026-7a4f827a568f","text":"4","replace":false,"value":["4"]}
[WD Proxy] Got an unexpected response: {"sessionId":"1a33069b-573c-4cd9-abf0-e72c9195c548","status":10,"value":"androidx.test.uiautomator.StaleObjectException\n\tat androidx.test.uiautomator.UiObject2.getAccessibilityNodeInfo(UiObject2.java:647)\n\tat androidx.test.uiautomator.UiObject2.hashCode(UiObject2.java:105)\n\tat java.lang.Obj...
[MJSONWP] Matched JSONWP error code 10 to StaleElementReferenceError
[W3C (012c27b7)] Encountered internal error running command: StaleElementReferenceError: androidx.test.uiautomator.StaleObjectException
[W3C (012c27b7)] at androidx.test.uiautomator.UiObject2.getAccessibilityNodeInfo(UiObject2.java:647)
[W3C (012c27b7)] at androidx.test.uiautomator.UiObject2.hashCode(UiObject2.java:105)
[W3C (012c27b7)] at java.lang.Object.toString(Object.java:273)
[W3C (012c27b7)] at java.util.Formatter$FormatSpecifier.printString(Formatter.java:2977)
[W3C (012c27b7)] at java.util.Formatter$FormatSpecifier.print(Formatter.java:2854)
[W3C (012c27b7)] at java.util.Formatter.format(Formatter.java:2523)
[W3C (012c27b7)] at java.util.Formatter.format(Formatter.java:2458)
[W3C (012c27b7)] at java.lang.String.format(String.java:2842)
[W3C (012c27b7)] at io.appium.uiautomator2.utils.ReflectionUtils.invoke(ReflectionUtils.java:71)
[W3C (012c27b7)] at io.appium.uiautomator2.core.AccessibilityNodeInfoGetter.fromUiObject(AccessibilityNodeInfoGetter.java:47)
[W3C (012c27b7)] at io.appium.uiautomator2.core.AccessibilityNodeInfoGetter.fromUiObject(AccessibilityNodeInfoGetter.java:41)
[W3C (012c27b7)] at io.appium.uiautomator2.utils.ElementHelpers.getText(ElementHelpers.java:234)
[W3C (012c27b7)] at io.appium.uiautomator2.utils.ElementHelpers.getText(ElementHelpers.java:217)
[W3C (012c27b7)] at io.appium.uiautomator2.model.UiObject2Element.getText(UiObject2Element.java:73)
[W3C (012c27b7)] at io.appium.uiautomator2.handler.SendKeysToElement.isTextFieldNotClear(SendKeysToElement.java:48)
[W3C (012c27b7)] at io.appium.uiautomator2.handler.SendKeysToElement.safeHandle(SendKeysToElement.java:87)
[W3C (012c27b7)] at io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:37)
[W3C (012c27b7)] at io.appium.uiautomator2.server.AppiumServlet.handleRequest(AppiumServlet.java:252)
[W3C (012c27b7)] at io.appium.uiautomator2.server.AppiumServlet.handleHttpRequest(AppiumServlet.java:243)
[W3C (012c27b7)] at io.appium.uiautomator2.http.ServerHandler.channelRead(ServerHandler.java:44)
[W3C (012c27b7)] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366)
[W3C (012c27b7)] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352)
[W3C (012c27b7)] at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345)
[W3C (012c27b7)] at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
[W3C (012c27b7)] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366)
[W3C (012c27b7)] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352)
[W3C (012c27b7)] at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345)
[W3C (012c27b7)] at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:435)
[W3C (012c27b7)] at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293)
[W3C (012c27b7)] at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:267)
[W3C (012c27b7)] at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:250)
[W3C (012c27b7)] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366)
[W3C (012c27b7)] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352)
[W3C (012c27b7)] at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345)
[W3C (012c27b7)] at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1294)
[W3C (012c27b7)] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366)
[W3C (012c27b7)] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352)
[W3C (012c27b7)] at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:911)
[W3C (012c27b7)] at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131)
[W3C (012c27b7)] at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:611)
[W3C (012c27b7)] at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:552)
[W3C (012c27b7)] at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:466)
[W3C (012c27b7)] at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:438)
[W3C (012c27b7)] at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:140)
[W3C (012c27b7)] at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
[W3C (012c27b7)] at java.lang.Thread.run(Thread.java:764)
[W3C (012c27b7)]
[W3C (012c27b7)] at errorFromMJSONWPStatusCode (/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-base-driver/lib/protocol/errors.js:786:12)
[W3C (012c27b7)] at ProxyRequestError.errorFromMJSONWPStatusCode [as getActualError] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-base-driver/lib/protocol/errors.js:683:14)
[W3C (012c27b7)] at JWProxy.getActualError [as command] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-base-driver/lib/jsonwp-proxy/proxy.js:235:19)
[HTTP] <-- POST /wd/hub/session/012c27b7-cf35-46ca-bb7b-8bcc791d4b56/element/720d3997-7d91-49b7-8026-7a4f827a568f/value 404 13042 ms - 9247
[HTTP]
1 Like

Hey, I am having the same issue now.
Did you ever find a solution to this?