Using ADB to access a particular UI control on the screen

后端 未结 5 1126
时光说笑
时光说笑 2021-02-01 07:12

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 dev

5条回答
  •  温柔的废话
    2021-02-01 08:02

    Yes you can, although it's not pretty.

    You can get the view hierarchy by using this command:

    adb exec-out uiautomator dump /dev/tty, which prints the output to the screen as an XML file. Unfortunately it's not valid XML, as there's some text appended. So let's filter that out:

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

    Now we've got a valid XML. Let's run this through 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, which will search for all nodes with the specific ID. Now you got the node, and you can do some more stuff on this.

    If you were to get only the viewbounds, you'd have to do this:

    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}', which cleans up the string afterwards and just outputs [294,1877][785,1981].

    The source of the specific node is:

提交回复
热议问题