1)下載、安裝python
1、進入python官網https://www.python.org/downloads/
2、點擊Download Python 3.7.3,即可下載python-3.7.3-amd64.exe
3、下載完成后,點擊即可安裝python
2)安裝uiautomator2
由於Python 3.7已經內置了pip,所以我們直接使用命令pip install --pre uiautomator2 即可安裝uiautomator2:
安裝完成后,默認安裝目錄為:C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Lib\site-packages\uiautomator2
3)使用USB連接設備,連接OK后運行命令python -m uiautomator2 init,該命令會自動安裝本庫所需要的設備端程序,也會在你的手機里安裝兩個軟件一個可見一個不可見
(有圖標和沒圖標)
4)安裝可視化UI查看器 pip install --pre -U weditor
5)打開CMD命令框,運行python -m weditor ,該命令會在PC上打開瀏覽器,之后輸入設備的ip或者序列號,點擊Connect即可,映射測試機屏幕

6)實例
import uiautomator2 as u2 import os import time #d = u2.connect('172.16.0.7') d = u2.connect_usb('12345678') #進入應用列表 d(resourceId="com.android.launcher3:id/all_apps_handle").click() time.sleep(5) # 啟動AppWWW d(resourceId="com.android.launcher3:id/icon", text=u"驍龍相機").click() time.sleep(5) # 拍照 d(resourceId="org.codeaurora.snapcam:id/shutter_button").click() time.sleep(5) # 切換攝像圖 d(resourceId="org.codeaurora.snapcam:id/front_back_switcher").click() time.sleep(5) #拍照 d(resourceId="org.codeaurora.snapcam:id/shutter_button").click() time.sleep(5) #點擊預覽按鈕 d(resourceId="org.codeaurora.snapcam:id/preview_thumb").click() time.sleep(5) #按返回鍵 for i in range(2): os.system("adb shell input keyevent 4") time.sleep(3)
