openatx / uiautomator2

Android Uiautomator2 Python Wrapper
MIT License
6.58k stars 1.4k forks source link

How to extract text from textview, resourceId is known in python #802

Closed JayPatelge closed 2 years ago

JayPatelge commented 2 years ago

How can i extract text or save it, from TextView when resourceId is known. currently am taking screenshot and using tesseract to extract text out of the image. but it takes lot of time and sometimes does mistakes as well. If am able to get text from resourceId then would save some time. have seen threads to do so with java, but want to use it in Python.

Thanks in advance.

MuzammilPatel commented 2 years ago

Hi @JayPatelge , If I have understood right, do you have the hierarchy of the UI in a screenshot form? for eg: <node index="0" text="" resource-id="android:id/list" class="android.widget.ListView" ...> <node index="0" text="WIRELESS & NETWORKS" resource-id="" class="android.widget.TextView" .../> <node index="1" text="" resource-id="" class="android.widget.LinearLayout" ...> <node index="1" text="" resource-id="" class="android.widget.RelativeLayout" ...> <node index="0" text="Wi‑Fi" resource-id="android:id/title" class="android.widget.TextView" .../> </node> <node index="2" text="ON" resource-id="com.android.settings:id/switchWidget" class="android.widget.Switch" .../> </node> ... </node>

You can use the dump_hierarchy() function to get the complete hierarchy of the current page and save it in *.xml file and then extract the contents.

mingyuan-xia commented 2 years ago

read the doc for get_text