pythonandroidappiumadb

Get children inside a android.view.ViewGroup via python


Is it possible to get child elements or text from android.view.ViewGroup using python, adb or any libraries, but without java and kotlin?

I'm trying to automate an android emulator using python. Interaction with the emulator occurs via adb. When I get a screen dump, I see that the elements I need are android.view.ViewGroup, and the text and content-desc properties are empty, although there is text on the screen. In different emulator instances, the elementId value for the same elements is different, so it is not suitable for identification. Using Appium Inspector, I get the same result.


Solution

  • As you haven't provided an example, I'm picking a random one. This is Contacts app, with a couple of contacts loaded.

    Using AndroidViewClient/culebra's dump -ah you can get the tree and check the properties of the children of the ViewGroup like in this snippet

    ...
                  {
                     "id": 39,
                     "parent": 38,
                     "text": "",
                     "package": "com.google.android.contacts",
                     "checkable": false,
                     "clickable": false,
                     "index": 0,
                     "content_description": "",
                     "focusable": false,
                     "resource_id": "",
                     "enabled": true,
                     "password": false,
                     "long_clickable": false,
                     "long_text": "android.view.ViewGroup__ id=39 parent=38",
                     "clazz": "android.view.ViewGroup",
                     "scrollable": false,
                     "selected": false,
                     "checked": false,
                     "focused": false,
                     "bounds": [
                       0,
                       391,
                       1080,
                       538
                     ],
                     "children": [
                       {
                         "id": 40,
                         "parent": 39,
                         "text": "C",
                         "package": "com.google.android.contacts",
                         "checkable": false,
                         "clickable": false,
                         "index": 0,
                         "content_description": "C",
                         "focusable": false,
                         "resource_id": "",
                         "enabled": true,
                         "password": false,
                         "long_clickable": false,
                         "long_text": "android.widget.TextView__C id=40 parent=39",
                         "clazz": "android.widget.TextView",
                         "scrollable": false,
                         "selected": false,
                         "checked": false,
                         "focused": false,
                         "bounds": [
                           0,
                           430,
                           147,
                           498
                         ],
                         "children": []
                       },
    ...