使用ADB访问屏幕上的特定UI控件

时间:2013-09-20 20:05:48

标签: android adb buttonclick

adb shell控制台是否可以使用其ID访问Android应用的特定按钮?还是文字?

我正在尝试自动按下设备上的按钮。这是从浏览器访问的Web应用程序。所以,如果我有那个按钮ID,我可以向该按钮发送一个动作吗?

5 个答案:

答案 0 :(得分:12)

UI automator提供资源ID,文本和UI元素的边界。可以使用XML查看器或Chrome浏览器更好地查看文件。

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

可以提取UI元素的边界并且可以计算中点。根据需要将text=替换为resource-id=content-desc=

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)

现在我们在$coords中拥有了UI元素中心的坐标,我们只需要发送一个点击事件。

adb shell input tap $coords

答案 1 :(得分:7)

我认为你能做的最好就是根据坐标注入触摸。

请参阅send touch event from ADB to a devicesimulating touch using ADB

您可以从dumpsys窗口或活动中获取按钮的坐标。

您还可以查看Monkeyrunner

答案 2 :(得分:4)

您将无法使用按钮ID,但您可以获取按钮坐标并模拟点击事件。

Android附带一个输入命令行工具,可以模拟各种输入事件。要模拟点击,请使用:

input tap x y

您可以使用adb shell远程运行命令:

adb shell input tap x y

其他选项包括:

shell@m0:/ $ input
input
usage: input ...
       input text <string>
       input keyevent <key code number or name>
       input [touchscreen|touchpad|touchnavigation] tap <x> <y>
       input [touchscreen|touchpad|touchnavigation] swipe <x1> <y1> <x2> <y2> [duration(ms)]
       input trackball press
       input trackball roll <dx> <dy>

答案 3 :(得分:0)

如果我使用python将cmd命令与subprocess.Popen结合使用,脚本将为:

from subprocess import Popen, PIPE

import os

adbpath = r'YOUR ADB DIR'
adbfile = 'adb.exe'
adb = os.path.join(adbpath, adbfile)

def tap(a):
    cor = a
    t = Popen([adb, 'shell', 'input', 'tap', str(a[0]), str(a[1])])

#i give you extra script

def swipe(a, b):
     first = a
     last = b
     sab = Popen([adb, 'shell', 'input', 'swipe', str(first[0]), str(first[1]), str(last[0]), str(last[1]), '200'])

然后,当您需要点击按钮的坐标时,只需执行以下操作:

ok_coord = 122,137
tap(ok_coord)

要滑动时:

coord1 = 122,373
coord2 = 122,26
swipe(coord1, coord2)

要获取按钮或点的坐标,请使用文件夹“ sdk \ tools \ bin”中的“ uiautomatorviewer.bat”

或者您可以使用杏子,erm3nda或Vinicius Dantas方法,

答案 4 :(得分:0)

是的,虽然不是很漂亮。

您可以使用以下命令获取视图层次结构:

adb exec-out uiautomator dump /dev/tty,它将输出作为XML文件输出到屏幕。不幸的是,由于添加了一些文本,因此它不是有效的XML。因此,让我们将其过滤掉:

adb exec-out uiautomator dump /dev/tty | awk '{gsub("UI hierchary dumped to: /dev/tty", "");print}'

现在我们有一个有效的XML。让我们通过XPath运行它:

adb exec-out uiautomator dump /dev/tty | awk '{gsub("UI hierchary dumped to: /dev/tty", "");print}' | xpath '//node[@resource-id="com.example:id/btnDone"] 2> /dev/null,它将搜索具有特定ID的所有节点。现在您有了节点,您可以在此做更多的事情。

如果仅获取视域,则必须执行以下操作:

adb exec-out uiautomator dump /dev/tty | awk '{gsub("UI hierchary dumped to: /dev/tty", "");print}' | xpath '//node[@resource-id="com.example:id/btnDone"]/@bounds' 2> /dev/null | awk '{$1=$1};1' | awk '{gsub("bounds=", "");print}' | awk '{gsub("\"", "");print}',此后将清理字符串并仅输出[294,1877][785,1981]

特定节点的来源是:

<node index="1" text="Let’s get started!" resource-id="com.example:id/btnDone" class="android.widget.Button" package="com.example" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[294,1877][785,1981]" />