這篇文章主要介紹了Python腳本利用adb進行手機控製的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下麵隨著小編來一起學習學習吧
一. adb 相關命令:
1. 關閉adb服務:adb kill-server
2. 啟動adb服務 adb start-server
3. 查詢當前運行的所有設備 adb devices
4. 可能在adb中存在多個虛擬設備運行 可以指定虛擬設備運行 -s 虛擬設備名稱
5. 重啟設備 adb reboot --指定虛擬設備 adb -s 設備名稱 reboot
6. 查看日誌 adb logcat 清除日誌 adb logcat -c
7. 進入linux shell下 adb shell 其中常用的linux命令 cd cat 等等 輸入su可以獲取超級管理員名稱了 要確定是否有哪些命令 進入 system/bin目錄 就知道了
8. 傳入文件到設備中 adb push 本地文件 遠程目錄
9. 從設備中拷貝文件到本地 adb -s emulator-5556 pull /data/config.ini d:/
10. 顯示當前運行的全部模擬器:
adb devices
1 安裝應用程序:
adb install -r 123.apk
12. 獲取模擬器中的文件:
adb pull <remote> <local>
13. 向模擬器中寫文件:
adb push <local> <remote>
14. 進入模擬器的shell模式:
adb shell
15. 卸載apk包:
adb shell
cd data/app
rm 123.apk
exit
adb uninstall 123.apk
adb install -r 123.apk
16. 查看adb命令幫助信息:
adb help
17. 刪除係統應用:
adb remount (重新掛載係統分區,使係統分區重新可寫)。
adb shell
cd system/app
rm 123.apk
18. 獲取管理員權限:
adb root
19、複製文件:
複製一個文件或目錄到設備:
adb push <source> <destination></destination></source>
如:adb push update.zip /sdcard/
從設備上複製一個文件或目錄:
adb pull <source> <destination></destination></source>
如:adb pull /sdcard/update.zip.
20、取得當前運行設備的實例的列表及每個實例的狀態:
adb devices
21:adb shell input tap
這條命令模擬Android手機在屏幕坐標(X,Y)處進行了點擊操作。
22:adb shell input swipe
這條命令模擬Android手機從屏幕坐標(X1,Y1)滑動到坐標(X2,Y2)的操作。
23、uiautomator dump dump: creates an XML dump of current UI hierarchy 這個命令是用來成成當前界麵的UI層次,並用XML格式進行展示 。這樣就可以獲取各個組件的位置了
注:如果PC要想同時控製多台Android手機,必須在adb 後麵添加-s
例如:adb -s 13b6e4c4 shell input tap 400 400
表示對13b6e4c4這台Android手機進行在屏幕上(400,400)坐標位置進行模擬的點擊事件。
24. 能看到設備信息就代表設備已經連接成功了,接下來的命令就是adb install 路徑+包名.apk
例如我的安裝包放在桌麵,那麼命令就是adb install C:\Users\hyh\Desktop\XXX.apk
*** adb shell uiautomator dump /mnt/sdcard/window_dump.xml 獲得手機當前界麵的UI信息,生成window_dump.xml
*** adb shell input text “123” 輸入text
舉例:
1、打開cmd,進入到當前文件夾中,輸入命令 adb devices 查看當前與電腦連接的設備(前提是,手機打開usb調試模式),可以查看已連接成功的手機。
2、 若手機成功連接,輸入命令 adb shell input tap 100 100 , 表示點擊屏幕上坐標為(100,100)的點,如果不知道需要點擊的點的具體位置的話可以在手機開發者模式中設置。
二. adb 模擬按鍵:
1. 比如使用 adb shell input keyevent <keycode> 命令,不同的 keycode 能實現不同的功能,完整的 keycode 列表詳見 KeyEvent,摘引部分我覺得有意思的如下:
keycode | 含義 |
---|---|
3 | HOME 鍵 |
4 | 返回鍵 |
5 | 打開撥號應用 |
6 | 掛斷電話 |
24 | 增加音量 |
25 | 降低音量 |
26 | 電源鍵 |
27 | 拍照(需要在相機應用裏) |
64 | 打開瀏覽器 |
82 | 菜單鍵 |
85 | 播放/暫停 |
86 | 停止播放 |
87 | 播放下一首 |
88 | 播放上一首 |
122 | 移動光標到行首或列表頂部 |
123 | 移動光標到行末或列表底部 |
126 | 恢複播放 |
127 | 暫停播放 |
164 | 靜音 |
176 | 打開係統設置 |
187 | 切換應用 |
207 | 打開聯係人 |
208 | 打開日曆 |
209 | 打開音樂 |
210 | 打開計算器 |
220 | 降低屏幕亮度 |
221 | 提高屏幕亮度 |
223 | 係統休眠 |
224 | 點亮屏幕 |
231 | 打開語音助手 |
276 | 如果沒有 wakelock 則讓係統休眠 |
2. input 命令的一些用法舉例
電源鍵
命令:
1 | adb shell input keyevent 26 |
執行效果相當於按電源鍵。
菜單鍵
命令:
1 | adb shell input keyevent 82 |
HOME 鍵
命令:
1 | adb shell input keyevent 3 |
返回鍵
命令:
1 | adb shell input keyevent 4 |
音量控製
增加音量:
1 | adb shell input keyevent 24 |
降低音量:
1 | adb shell input keyevent 25 |
靜音:
1 | adb shell input keyevent 164 |
媒體控製
播放/暫停:
1 | adb shell input keyevent 85 |
停止播放:
1 | adb shell input keyevent 86 |
播放下一首:
1 | adb shell input keyevent 87 |
播放上一首:
1 | adb shell input keyevent 88 |
恢複播放:
1 | adb shell input keyevent 126 |
暫停播放:
1 | adb shell input keyevent 127 |
點亮/熄滅屏幕
可以通過上文講述過的模擬電源鍵來切換點亮和熄滅屏幕,但如果明確地想要點亮或者熄滅屏幕,那可以使用如下方法。
點亮屏幕:
1 | adb shell input keyevent 224 |
熄滅屏幕:
1 | adb shell input keyevent 223 |
三、使用python腳本自動運行cmd 命令
在adb文件夾下建立一個python文件
1 2 | import os os.system( 'adb shell input tap 100 100' ); |
運行腳本,發現與在命令行輸入相同語句有同樣的效果。
2、也可以使用subprocess.Popen,最簡單使用方式如下,設置shell=True,就不會彈出cmd框
1 | process = subprocess.Popen( 'adb shell input tap 14 1402' ,shell = True ) |
程序實例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #coding:utf-8 #程序功能:可以實現抖音個人頁麵視頻的自動點擊,從而自動增加訪問量 #思路:抖音主頁中兩個視頻切換點擊,可以實現訪問量的增加 #使用ADB程序,視頻的屏幕坐標可以使用adb shell uiautomator dump命令,獲取該頁麵xml源碼後查得 #下述是小米MIX2抖音主頁第一個視頻和第二個視頻的坐標位置 #缺點:運行時不能移動屏幕,後續可以采用獲取模塊ID號的方式去點擊相應的位置 import time import subprocess i = 0 #每次操作的間隔時間取決於手機配置,配置越高時間越短 sleep_time = 0.5 while 1 : #用popen設置shell=True不會彈出cmd框 process = subprocess.Popen( 'adb shell input tap 14 1402' ,shell = True ) time.sleep(sleep_time) process = subprocess.Popen( 'adb shell input keyevent KEYCODE_BACK' , shell = True ) time.sleep(sleep_time) process = subprocess.Popen( 'adb shell input tap 375 1402' , shell = True ) time.sleep(sleep_time) process = subprocess.Popen( 'adb shell input keyevent KEYCODE_BACK' , shell = True ) time.sleep(sleep_time) #os.system('adb shell input tap 14 1402') #os.system('adb shell input keyevent KEYCODE_BACK') #os.system('adb shell input tap 375 1402') i + = 1 print str (i) + 'clicks have been completed' |
實現原理
Hierarchy Viewer:獲得當前手機實時的UI信息,方便用於手機的自動化測試;
python中的subprocess.Popen() 或 Python os模塊:調用係統命令;
uiautomator工具:獲取界麵控件信息;
adb命令:對手機進行操作。