Using ADB to access a particular UI control on the screen

Siddharthan Asokan picture Siddharthan Asokan · Sep 20, 2013 · Viewed 36.8k times · Source

Is it possible for adb shell console to access an Android app's specific button using its id? or text?

I'm trying to automate the button click on the device. This is a web app accessed from a browser. So if I have that button id can I send an action to that button?

Answer

apricot picture apricot · Apr 25, 2018

UI automator gives resource IDs, text, and the bounds of the UI element. An XML viewer or Chrome browser can be used to get a better view of the file.

adb pull $(adb shell uiautomator dump | grep -oP '[^ ]+.xml') /tmp/view.xml

The UI element's bounds can be be extracted and the mid point can be calculated. Replace text= with resource-id= or content-desc= as needed.

coords=$(perl -ne 'printf "%d %d\n", ($1+$3)/2, ($2+$4)/2 if /text="MY_BUTTON_TEXT"[^>]*bounds="\[(\d+),(\d+)\]\[(\d+),(\d+)\]"/' /tmp/view.xml)

Now we have the coordinates of the UI element's center in $coords and we just need to send a tap event.

adb shell input tap $coords