作者:虛壞叔叔
博客:https://xuhss.com
早餐店不會開到晚上,想吃的人早就來了!
uiautomator
服務import uiautomator2 as u2
import time
# Connected by the serial number of the mobile phone
d = u2.connect_usb("127.0.0.1:62001")
print(d.service("uiautomator").running())
# 通過start方法啟動uiautomator服務
d.service("uiautomator").start()
time.sleep(2)
print(d.service("uiautomator").running())
Run the output data:
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Administrator/Desktop/u2_project/testu2.py
False
True
The phone virtual machine also shows that the service starts:
To stop the service, you only need to call it like thisstop方法即可
d.service("uiautomator").stop()
uiautomator
並沒有提供atx
Start and stop related operations,But provides an interface to view the status,可以手動關閉atx-agent.
But better not to stopatx-agent,如果停止,The program cannot control the mobile device.
# 查看atx-agent運行狀態
print(d.agent_alive)
如果atx-agent停止了,可以通過connect_usbThe code wakes up again:
d = u2.connect_usb("127.0.0.1:62001")
當atx-agent和uiautomatorare all running
We can view the data of the device,If you find that you can't view it,It is the service that stops running,You can start this by 2It depends on the service
# 查看設備數據
print(d.device_info)
輸出數據如下:
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Administrator/Desktop/u2_project/testu2.py
True
{'udid': 'android-08:00:27:77:f5:4d-AOSP_on_Shama', 'version': '5.1.1', 'serial': 'android', 'brand': 'Android', 'model': 'AOSP on Shama', 'hwaddr': '08:00:27:77:f5:4d', 'sdk': 22, 'agentVersion': '0.10.0', 'display': {'width': 1080, 'height': 1920}, 'battery': {'acPowered': False, 'usbPowered': True, 'wirelessPowered': False, 'status': 5, 'health': 2, 'present': True, 'level': 90, 'scale': 100, 'voltage': 10000, 'temperature': 378, 'technology': 'Li-ion'}, 'memory': {'total': 3566000, 'around': '3 GB'}, 'arch': '', 'owner': None, 'presenceChangedAt': '0001-01-01T00:00:00Z', 'usingBeganAt': '0001-01-01T00:00:00Z', 'product': None, 'provider': None}
可以獲取app
# 安裝apk
d.app_install(data="C:\\Users\\Administrator\\Downloads\\3.5.6.apk")
啟動appNeed to know the package name,包名可以通過aapt獲取:
cd /d C:\Program Files (x86)\Nox\bin
aapt dump badging C:\Users\Administrator\Downloads\3.5.6.apk
然後啟動app
:
# 啟動app
d.app_start(package_name="com.tal.kaoyan")
# View what is currently running in the foregroundapp數據
print(d.app_current())
輸出數據:
{'package': 'com.tal.kaoyan', 'activity': 'com.kaoyan.kylogin.ui.login.LoginActivity'}
# 停止app運行
d.app_stop(package_name="com.tal.kaoyan")
# 清理app緩存
d.app_clear(package_name="com.tal.kaoyan")
# 卸載app
d.app_uninstall(package_name="com.tal.kaoyan")
# Get current device ownershipapp數據
print(d.app_list())
['com.github.uiautomator', 'com.android.providers.telephony', 'com.android.providers.calendar', 'com.android.providers.media', 'com.android.wallpapercropper', 'com.android.documentsui', 'com.android.externalstorage', 'com.android.htmlviewer', 'com.android.mms.service', 'com.android.providers.downloads', 'com.github.uiautomator.test', 'com.android.browser', 'com.android.soundrecorder', 'com.android.defcontainer', 'com.android.providers.downloads.ui', 'com.android.pacprocessor', 'com.android.certinstaller', 'android', 'com.android.contacts', 'com.android.camera2', 'com.android.backupconfirm', 'com.android.provision', 'com.android.phasebeam', 'com.android.providers.settings', 'com.android.sharedstoragebackup', 'com.android.dreams.basic', 'com.android.inputdevices', 'com.google.android.webview', 'com.android.onetimeinitializer', 'com.android.server.telecom', 'com.android.keychain', 'com.android.inputservice', 'com.android.gallery3d', 'com.android.packageinstaller', 'com.svox.pico', 'com.android.proxyhandler', 'com.cyanogenmod.filemanager', 'com.android.managedprovisioning', 'com.android.wallpaper.livepicker', 'com.android.settings', 'com.android.vpndialogs', 'com.android.phone', 'com.android.shell', 'com.android.providers.userdictionary', 'com.android.location.fused', 'com.android.systemui', 'com.android.bluetooth', 'com.android.providers.contacts', 'com.android.captiveportallogin']
# 獲取正在運行的app列表
print(d.app_list_running())
['com.android.externalstorage', 'com.github.uiautomator', 'com.android.providers.calendar', 'com.android.systemui', 'com.android.defcontainer', 'com.android.inputservice', 'com.android.documentsui', 'com.android.keychain', 'com.svox.pico', 'com.android.phone']
# 停止所有app運行
d.app_stop_all()
# 卸載所有app
d.app_uninstall_all()
點贊
收藏
轉發
一波哦,博主也支持為鐵粉絲制作專屬動態壁紙哦~python3調用百度翻譯接口
C++讀取python腳本中的dictionary字典變量以及它的鍵值對
Qt中的套件提示no complier set in kit和no debugger,出現黃色感歎號問題解決(MSVC2017)
Python+selenium 自動化 - 實現自動導入、上傳外部文件(不彈出windows窗口)
關注下面卡片即刻獲取更多編程知識,包括各種語言學習資料,上千套PPT模板和各種游戲源碼素材等等資料.更多內容可自行查看哦!