web-dev-qa-db-ja.com

ADBを使用して画面上の特定のUIコントロールにアクセスする

adb Shell Androidアプリの特定のボタンにアクセスするには、ID?またはテキスト?

デバイスのボタンクリックを自動化しようとしています。ブラウザからアクセスするウェブアプリです。そのボタンIDがある場合、そのボタンにアクションを送信できますか?

13

座標に基づいてタッチを注入するのがベストだと思います。

ADBからデバイスにタッチイベントを送信する および ADBを使用してタッチをシミュレートする を参照してください

Dumpsysウィンドウまたはアクティビティからボタンの座標を取得する場合があります。

Monkeyrunner もチェックアウトできます。

8
pt2121

UIオートマトンは、リソース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)

これで、UI要素の中心の座標が$coordsそしてタップイベントを送信するだけです。

adb Shell input tap $coords
20
apricot

はい、できますが、きれいではありません。

次のコマンドを使用して、ビューの階層を取得できます。

_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]" />_

5
Niels

ボタンIDは使用できませんが、ボタンの座標を取得してタップイベントをシミュレートできます。

Androidには、さまざまな入力イベントをシミュレートできる入力コマンドラインツールが付属しています。タッピングをシミュレートするには、以下を使用します。

input tap x y

Adbシェルを使用して、コマンドをリモートで実行できます。

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>
4
Pedro Lobito

私の場合、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メソッドを使用できます。

1
Wahyu Bram