'How to extract text from textview, resourceId is known in python
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.
Solution 1:[1]
You can use AndroidViewClient/culebra to use python to get the text of a TextView knowing its resource id (in the example I'm using the id for the date widget):
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from com.dtmilano.android.viewclient import ViewClient
res = "com.google.android.apps.nexuslauncher:id/clock"
print(ViewClient(*ViewClient.connectToDeviceOrExit()).findViewByIdOrRaise(res).getText())
edit
The error you mentioned:
ValueError: received does not contain valid XML: Killed
is, unfortunately, a limitation of the UiAutomator backend. However, AndroidViewClient/culebra can choose a different backend in this an other situations.
You can follow the steps described at CulebraTester2-public and a few simple changes to the above script to account for some differences in the API (notice useuiautomatorhelper=True):
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from com.dtmilano.android.viewclient import ViewClient
res = "com.google.android.apps.nexuslauncher:id/clock"
vc = ViewClient(*ViewClient.connectToDeviceOrExit(), useuiautomatorhelper=True)
print(vc.uiAutomatorHelper.ui_object2.get_text(vc.findViewByIdOrRaise(res).oid).text)
Solution 2:[2]
The solution, using Uiautomator is as simple, but it took me so long to figure it out.
text = device(resourceId="your resourceId").get_text()`
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Jay Patel |
