使用 uiautomator2
環境:
- Android: 4.4+
- Python:3.7.10
uiautomator2:https://github.com/openatx/uiautomator2
安裝adb
官網地址:https://developer.android.google.cn/studio/command-line/adb
準備一臺開啟了開發者選項的安卓手機,通過USB連接上電腦
電腦上通過以下命令查看已連接的設備
adb devices -l(Python37) M:\python>adb devices -l
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
List of devices attached
xxxxxxxx device product:oxygen model:MI_MAX_2 device:oxygen
安裝 weditor
pip install --pre --upgrade weditor(Python37) M:\python>pip install --pre --upgrade weditor
Collecting weditor
Downloading weditor-0.6.4-py3-none-any.whl (1.6 MB)
|████████████████████████████████| 1.6 MB 726 kB/s
Collecting facebook-wda>=1.0.5
Downloading facebook_wda-1.4.3-py3-none-any.whl (25 kB)
Requirement already satisfied: pillow in h:\anacondanavigator\anaconda\envs\python37\lib\site-packages (from weditor) (8.2.0)
…
…
…
Successfully installed facebook-wda-1.4.3 pywin32-301 weditor-0.6.4
啟動 weditor
python -m weditor注:命令行直接輸入 weditor會自動打開瀏覽器,輸入設備的ip或者序列號,點擊Connect即可。
默認會通過瀏覽器打開頁面:http://localhost:17310/
初始化所有的已經連接到電腦的設備
或者 初始化所有的已經連接到電腦的設備 并指定端口號
python -m uiautomator2 init --addr :7912(Python37) C:\Users\vvcat>python -m uiautomator2 init --addr :7912
[I 210917 13:27:42 init:156] uiautomator2 version: 2.16.7
[I 210917 13:27:42 init:356] Install minicap, minitouch
[D 210917 13:27:42 init:59] Use cached assets: C:\Users\vvcat.uiautomator2\cache\minitouch-f7a806902f\minitouch
[D 210917 13:27:42 init:238] Push to /data/local/tmp/minitouch:0755
[D 210917 13:27:42 init:59] Use cached assets: C:\Users\vvcat.uiautomator2\cache\minicap.so-14ea849b4c\minicap.so
[D 210917 13:27:42 init:238] Push to /data/local/tmp/minicap.so:0755
[D 210917 13:27:42 init:59] Use cached assets: C:\Users\vvcat.uiautomator2\cache\minicap-4ad740a772\minicap
[D 210917 13:27:42 init:238] Push to /data/local/tmp/minicap:0755
[D 210917 13:27:43 init:256] apk-debug package-info: {‘package_name’: ‘com.github.uiautomator’, ‘version_name’: ‘2.3.3’, ‘version_code’: ‘2003003’, ‘flags’: [‘DEBUGGABLE’, ‘HAS_CODE’, ‘ALLOW_CLEAR_USER_DATA’, ‘ALLOW_BACKUP’], ‘first_install_time’: datetime.datetime(2021, 9, 2, 19, 36, 3), ‘last_update_time’: datetime.datetime(2021, 9, 2, 19, 36, 3), ‘signature’: ‘ae17cd86’}
[D 210917 13:27:43 init:257] apk-debug-test package-info: {‘package_name’: ‘com.github.uiautomator.test’, ‘version_name’: ‘’, ‘version_code’: ‘’, ‘flags’: [‘DEBUGGABLE’, ‘HAS_CODE’, ‘ALLOW_CLEAR_USER_DATA’, ‘ALLOW_BACKUP’], ‘first_install_time’: datetime.datetime(2021, 9, 2, 19, 36, 10), ‘last_update_time’: datetime.datetime(2021, 9, 2, 19, 36, 10), ‘signature’: ‘ae17cd86’}
[I 210917 13:27:43 init:374] Already installed com.github.uiautomator apks
[D 210917 13:27:43 init:167] Shell: (’/data/local/tmp/atx-agent’, ‘server’, ‘–stop’)
[D 210917 13:27:43 init:296] Real version: [0, 10, 0], Expect version: [0, 10, 0]
[D 210917 13:27:43 init:167] Shell: (’/data/local/tmp/atx-agent’, ‘server’, ‘–nouia’, ‘-d’, ‘–addr’, ‘:7912’)
[I 210917 13:27:43 init:384] Check atx-agent version
[D 210917 13:27:43 init:397] Forward: local:tcp:1457 -> remote:tcp:7912
[D 210917 13:27:44 init:397] Forward: local:tcp:1457 -> remote:tcp:7912
[D 210917 13:27:45 init:400] atx-agent version 0.10.0
[D 210917 13:27:45 init:403] device wlan ip: 192.168.31.106
Successfully init AdbDevice(serial=xxxxxxxx)
測試:
打印通過usb連接獲取到的手機信息
(Python37) M:\python>python main.py
{‘currentPackageName’: ‘com.miui.home’, ‘displayHeight’: 1920, ‘displayRotation’: 0, ‘displaySizeDpX’: 393, ‘displaySizeDpY’: 698, ‘displayWidth’: 1080, ‘productName’: ‘oxygen’, ‘screenOn’: True, ‘sdkInt’: 25, ‘naturalOrientation’: True}
uiautomator2:https://github.com/openatx/uiautomator2/blob/master/README.md
找到 package 名字
啟動app
d.app_start("com.bilibili.ydydja.bili")關閉app
d.app_stop("com.bilibili.ydydja.bili")解鎖屏幕
def open_the_screen():d.info.get('screenOn') # 獲取當前屏幕狀態d.screen_on() # 打開屏幕d.swipe_ext("up", scale=0.8) # 默認0.9, 滑動距離為屏幕寬度的80% 從下往上滑動d.click(0.497, 0.531) # 解鎖界面數字坐標d.click(0.502, 0.818) # 解鎖界面數字坐標d.click(0.778, 0.629) # 解鎖界面數字坐標d.click(0.778, 0.629) # 解鎖界面數字坐標- click():點擊屏幕
- screen_on():打開屏幕
- screen_off():關閉屏幕
- swipe_ext():手指滑動
- left:左滑
- right:右滑
- up:上滑
- down:下滑
- info.get():獲取當前屏幕狀態
總結
以上是生活随笔為你收集整理的使用 uiautomator2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL Server 学习笔记
- 下一篇: SQL Server 练习