
1. Install package uiautomator2, and other necessary packages in advance
pip install uiautomator2 pip install pillow pip install -U weditor
2. Install adb
The full name of adb is called Android Debug Bridge ( Android Debug Bridge), ADB(Android Debug Bridge) is a tool provided by Google manage Android device from computer. it is included in Android Studio by default, but we can also download it directtly
The download link is as follows:
SDK Platform Tools Release Notes | Android Developers | Android Developers
Android SDK Platform-Tools is a component of the Android SDK.
https://developer.android.google.cn/studio/releases/platform-tools?hl=en_cn
3. Connect device through ADB
Connect the phone to the computer via a USB cable, enable developer mode, enable USB debugging, and enable permissions such as allowing the computer to install software to the phone via USB. Receive the link key from the computer and link.
(When using the Python package uiautomator2 to control the phone, you need to install the app on the phone)
Enter the following command in the console:
adb devices
This will list the connected devices
The expected result:
C:\Users\Administrator>adb devices
List of devices attached
9phqaetw device
afdsaf4 device
Code language: PHP (php)
4, Install ATX client on device
Run the following command from the command line to install the ATX software on the phone. If this APP has not installed on the phone, uiautomator2 will not be able to control the phone.
python -m uiautomator2 init
5. Python code to control the phone
import uiautomator2 as u2
# Connect to device, 9phqaetw is the one from `adv devices`
d = u2.connect('9phqaetw')
d.unlock() # unlock screen
x,y = 100,100
d.click(x,y) # click on x,y
# double click
d.double_click(x,y)
# long press
d.long_click(x,y)
#
d(text="9").click()
Code language: PHP (php)
We can also unlock screen with a password
assuming the lock screen password is 486570:
import uiautomator2 as u2
import time
d = u2.connect('9phqaetw')
d.unlock()
time.sleep(2)
password = "486570"
for c in password:
d(text=c).click()
time.sleep(0.3)
Code language: JavaScript (javascript)
More ways to use
After the above code runs successfully, you can basically control the phone. uiautomator2 has many functions, I won’t repeat them here. Just Google it