前言:
很多時候圣贸,想要學習一個陌生領(lǐng)域的知識時殴泰,需要先定一個或基礎(chǔ)或高階的目的于宙,這個目標最好帶有趣味性,然后錨定這個目標悍汛;或許最終目標很輕松就被實現(xiàn)了捞魁,也或許最終發(fā)現(xiàn)目標實在太高難以實現(xiàn);這些都沒有關(guān)系离咐,一步步探索谱俭,一步步走進目標的過程并在這個過程中得到提升才是學習的真正意義所在。
在一步步試探的過程中宵蛀,我們會思考昆著、搜索資料、質(zhì)疑是否有更好的實現(xiàn)方式糖埋;這一路的過程可以概括為:
——了解它
——走近它
——觀察它/測試它
——改造它/思考測試
——創(chuàng)造另一個它
只要自己不停止研究宣吱,目標只會越來越近窃这;正所謂:日拱一卒瞳别,功不唐捐是也征候。
一、下載安裝python
https://www.python.org/downloads/windows/
根據(jù)自己的OS版本選擇安裝
二祟敛、安裝uiautomator2包
安裝uiautomator2
pip install uiautomator2 -i https://pypi.douban.com/simple/</pre>
或者(建議在virtualenv下操作)
pip install --pre -U uiautomator2
需要較長時間0贪印!9萏跑揉!
三、安裝ADB工具
ADB下載路徑
Windows版本:https://dl.google.com/android/repository/platform-tools-latest-windows.zip
Mac版本:https://dl.google.com/android/repository/platform-tools-latest-darwin.zip
Linux版本:https://dl.google.com/android/repository/platform-tools-latest-linux.zip
將文件下載下來埠巨,解壓縮到自定義的安裝目錄即可历谍。
也可以直接安裝Android SDK;在Android SDK安裝目錄下的platform-tools文件夾中可以找到adb.exe程序辣垒。
四望侈、對移動設備執(zhí)行初始化
首先確保
1.手機設備成功接通到電腦(USB或WIFI);
2.手機開啟了調(diào)試模式勋桶;
以下幾項權(quán)限比較重要:
——開啟開發(fā)者模式(必要)
——USB調(diào)試(必要)
——USB安裝(必要)
——USB模擬點擊(必要)
——指針位置(建議開啟脱衙,非強制)
沒開啟前,我們再電腦上使用adb devices看到的結(jié)果是這樣的:
3.電腦上安裝adb工具(包含在Android SDK中例驹,也可以單獨安裝)捐韩;建議架構(gòu)adb工具為位置加入環(huán)境變量PATH
注意:下圖的顯示是根據(jù)你手機啟用的調(diào)試模式顯示的
USB調(diào)試是這樣:
對移動設備初始化
這個是最重要的操作。
這個過程會在移動設備上鹃锈,安裝一個agent(需要手機調(diào)試模式中允許調(diào)試安裝app)
python -m uiautomator2 init
如果提示(這個很重要荤胁,事個常見報錯)
ModuleNotFoundError: No module named 'dataclasses'
就安裝這個庫
pip install dataclasses
再次運行就可以看到安裝成功了:(有些手機需要插入SIM卡才可以開啟調(diào)試模式下安裝軟件的功能;根據(jù)要求滿足開啟條件就行屎债。)
(myenv) PS D:\xiamingliang\project001> python -m uiautomator2 init
[I 221022 09:30:55 init:156] uiautomator2 version: 2.16.19
[I 221022 09:30:55 init:373] Install minicap, minitouch
[D 221022 09:30:55 init:59] Use cached assets: C:\Users\Administrator.DESKTOP-QBI8U5R\.uiautomator2\cache\minitouch-f7a806902f\minitouch
[D 221022 09:30:55 init:238] Push to /data/local/tmp/minitouch:0755
[I 221022 09:30:55 init:379] Android R (sdk:30) has no minicap resource
[D 221022 09:30:56 init:256] apk-debug package-info: None
[D 221022 09:30:56 init:257] apk-debug-test package-info: None
[I 221022 09:30:56 init:388] Install com.github.uiautomator, com.github.uiautomator.test 2.3.3
[D 221022 09:30:56 init:167] Shell: ('pm', 'uninstall', 'com.github.uiautomator')
[D 221022 09:30:56 init:167] Shell: ('pm', 'uninstall', 'com.github.uiautomator.test')
[D 221022 09:30:56 init:59] Use cached assets: C:\Users\Administrator.DESKTOP-QBI8U5R\.uiautomator2\cache\app-uiautomator.apk-d3f17174fb\app-uiautomator.apk
[D 221022 09:30:56 init:238] Push to /data/local/tmp/app-uiautomator.apk:0644
[D 221022 09:30:57 init:167] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator.apk')
[I 221022 09:31:16 init:331] - app-uiautomator.apk installed
[D 221022 09:31:16 init:59] Use cached assets: C:\Users\Administrator.DESKTOP-QBI8U5R\.uiautomator2\cache\app-uiautomator-test.apk-652bf9e13c\app-uiautomator-test.apk
[D 221022 09:31:16 init:238] Push to /data/local/tmp/app-uiautomator-test.apk:0644
[D 221022 09:31:16 init:167] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator-test.apk')
[I 221022 09:31:23 init:331] - app-uiautomator-test.apk installed
[D 221022 09:31:23 init:167] Shell: ('/data/local/tmp/atx-agent', 'server', '--stop')
[I 221022 09:31:23 init:346] Install atx-agent 0.10.0
[D 221022 09:31:23 init:59] Use cached assets: C:\Users\Administrator.DESKTOP-QBI8U5R\.uiautomator2\cache\atx-agent_0.10.0_linux_armv7.tar.gz-cf3333ae7b\atx-agent_0.10.0_linux_armv7.tar.gz
[D 221022 09:31:23 init:238] Push to /data/local/tmp/atx-agent:0755
[D 221022 09:31:24 init:167] Shell: ('/data/local/tmp/atx-agent', 'server', '--nouia', '-d', '--addr', '127.0.0.1:7912')
[I 221022 09:31:24 init:350] Check atx-agent version
[D 221022 09:31:24 init:359] Forward: local:tcp:15957 -> remote:tcp:7912
[D 221022 09:31:25 init:359] Forward: local:tcp:15957 -> remote:tcp:7912
[D 221022 09:31:25 init:362] atx-agent version 0.10.0
[D 221022 09:31:25 init:366] device wlan ip: 192.168.88.105
Successfully init AdbDevice(serial=c******0)
(myenv) PS D:\xiamingliang\project001>
并且寨蹋,在你的手機上,也會有一個ATX
所以剛才我們輸入的uiautomator init
命令會執(zhí)行哪些操作呢 扔茅?
他會安裝3個包已旧,
uiautomator-server:谷歌原生的uiautomator
atx-agent:uiautomator的守護進程
atx-agent增加遠程控制的功能,依賴minicap和minitouch這兩個工具
初始化完成后召娜,既可以在這個目錄下C:\Users\Administrator\.uiautomator2\cache
看到這幾個包了:(這個不重要运褪,看不到也沒啥影響的)
五、連接手機
1.通過USB方式
>>>> import uiautomator2
>>> conn = uiautomator2.connect_usb('c******0') #adb devices顯示的設備id
>>> print(conn.info)
[D 221022 09:42:32 __init__:656] [pid:10108] [c******0] kill process(ps): uiautomator
[D 221022 09:42:33 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 40.0s
[D 221022 09:42:34 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 39.0s
[D 221022 09:42:35 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 37.9s
[D 221022 09:42:36 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 36.9s
[D 221022 09:42:37 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 35.9s
[D 221022 09:42:38 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 34.9s
[D 221022 09:42:39 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 33.8s
[D 221022 09:42:40 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 32.8s
[D 221022 09:42:41 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 31.8s
[D 221022 09:42:42 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 30.8s
[D 221022 09:42:42 __init__:691] [pid:10108] [c******0] show float window
[D 221022 09:42:43 __init__:678] [pid:10108] [c******0] uiautomator-v2 is starting ... left: 29.6s
[I 221022 09:42:43 __init__:638] [pid:10108] [c******0] uiautomator back to normal
{'currentPackageName': 'com.miui.home', 'displayHeight': 2276, 'displayRotation': 0, 'displaySizeDpX': 393, 'displaySizeDpY': 873, 'displayWidth': 1080, 'productName': 'munch', 'screenOn': True, 'sdkInt': 31, 'naturalOrientation': True}
>>>
可以看到設備的信息可以這個正常獲取并顯示玖瘸。
2.通過WIFI方式
>>>> import uiautomator2
>>> conn = uiautomator2.connect_adb_wifi('your phone ip address')
>>> print(conn.info)
3.uiautomator2其他常用的function
function:
_fix_wifi_addr:
_is_tmq_production:
connect: Args:
connect_adb_wifi: Run adb connect, and then call connect_usb(..)
connect_usb: Args:
connect_wifi: Args:
deprecated: This is a decorator which can be used to mark functions
list2cmdline:
namedtuple: Returns a new subclass of tuple with named fields.
process_safe_wrapper: threadsafe for process calls
retry: Returns a retry decorator.
setup_logger: Configures and returns a fully configured logger instance, no hassles.
thread_safe_wrapper:
六:用python做個調(diào)試程序
import uiautomator2
import time
import random
def douyin():
#通過usb連接;c******0是adb devices -l列出的被調(diào)試手機id(手機上要點擊允許)
conn=uiautomator2.connect_usb('c******0') #替換成自己的
# 打開抖音
conn(text="抖音").click() #沒打開時使用秸讹,打開程序后就不需要了
runcount_init = 0
runcount_all = 10 #劃幾次
while runcount_init < runcount_all:
# 滑動視頻
# 向下滑
conn.swipe(round(random.uniform(700,720),2),round(random.uniform(1650,1670),2),round(random.uniform(830,850),2),round(random.uniform(300,320),2),duration=random.uniform(0.01,0.1)) #調(diào)整duration控制滑動速度
time.sleep(random.uniform(0.5,1.5))
# 向上滑
conn.swipe(round(random.uniform(810,800),2),round(random.uniform(1160,1200),2),round(random.uniform(775,785),2),round(random.uniform(1750,1800),2),duration=random.uniform(0.01,0.1))
time.sleep(random.uniform(0.5,1.5))
runcount_init = runcount_init + 1
if __name__=='__main__':
douyin()
七:補充信息
幾種常用的定位APP方式如下:
1.text:a(text=“抖音”).click() #需要程序在屏幕最上層(如果APP在抽屜或者不在當前頁面則無法定位并打開的)
2.resourceid:a(resourceId=“com.smartisanos.clock:id/text_stopwatch”).click()
3.classname:a(className=“android.widget.TextView”).click()
4.description :a(description="…").click()</pre>
更多的方法參考:
名稱 | 描述 |
---|---|
text | text是指定文本的元素 |
textContains | text中包含有指定文本的元素 |
textMatches | text符合指定正則的元素 |
textStartsWith | text以指定文本開頭的元素 |
className | className是指定類名的元素 |
classNameMatches | className類名符合指定正則的元素 |
description | description是指定文本的元素 |
descriptionContains | description中包含有指定文本的元素 |
descriptionMatches | description符合指定正則的元素 |
descriptionStartsWith | description以指定文本開頭的元素 |
checkable | 可檢查的元素,參數(shù)為True雅倒,F(xiàn)alse |
checked | 已選中的元素璃诀,通常用于復選框,參數(shù)為True蔑匣,F(xiàn)alse |
clickable | 可點擊的元素劣欢,參數(shù)為True棕诵,F(xiàn)alse |
longClickable | 可長按的元素,參數(shù)為True凿将,F(xiàn)alse |
scrollable | 可滾動的元素校套,參數(shù)為True,F(xiàn)alse |
enabled | 已激活的元素牧抵,參數(shù)為True笛匙,F(xiàn)alse |
focusable | 可聚焦的元素,參數(shù)為True犀变,F(xiàn)alse |
focused | 獲得了焦點的元素妹孙,參數(shù)為True,F(xiàn)alse |
selected | 當前選中的元素获枝,參數(shù)為True涕蜂,F(xiàn)alse |
packageName | packageName為指定包名的元素 |
packageNameMatches | packageName為符合正則的元素 |
resourceId | resourceId為指定內(nèi)容的元素 |
resourceIdMatches | resourceId為符合指定正則的元素 |
對于元素的定位還支持:
1.子元素定位
2.兄弟定位
3.相對定位
4.XPATH定位等
八、視頻效果
點擊這里查看視頻效果映琳。
本文作者:夏明亮
轉(zhuǎn)載請標注出處: http://www.reibang.com/u/b637fb6e14fe