1. 安裝ADB
地址:https://developer.android.google.cn/studio/releases/platform-tools?hl=en
下載對應環境的工具 解壓
之后設置環境變量
右鍵我的電腦->屬性 然后按下圖順序 最后添加platform-tools的路徑保存即可
設置好后cmd打開命令行
輸入adb version
可以查看adb版本信息 ok~
2. 安裝uiautomator2
python下載uiautomator2包
pip install --pre uiautomator2
使用數據線將手機與電腦相連 使用開發者模式
在電腦命令行輸入
python -m uiautomator2 init
進行初始化
手機同一安裝atx 和com.github.uiautomator.test
如果需要wifi連接 adb需要tcpip連接模式
所以在數據線連接時我們需要設定端口
adb tcpip 5566
3.測試連接
手機與電腦連接同一個局域網
手機連接wifi后 ip為 192.168.0.102
adb連接手機設備
python測試如下
import uiautomator2 as u2 #d = u2.connect() # 有線連接,手機需要插電腦上 d = u2.connect("192.168.0.102:5566") #通過無線連接,電腦和手機需要在同一個局域網內,并且需要先用有線的方式做過初始化 print(d.info)
輸出設備信息
{‘currentPackageName": ‘com.bbk.launcher2", ‘displayHeight": 2160, ‘displayRotation": 0, ‘displaySizeDpX": 360, ‘displaySizeDpY": 720, ‘displayWidth": 1080, ‘productName": ‘PD1709", ‘screenOn": True, ‘sdkInt": 27, ‘naturalOrientation": True}
連接成功
到此這篇關于python 使用uiautomator2連接手機設備的實現的文章就介紹到這了,更多相關python uiautomator2連接手機設備內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/zkt286468541/article/details/113986436