python實(shí)現(xiàn)電腦操控安卓手機(jī)
一、電腦下載并安裝SDK Platform Tools
下載后的文件:platform-tools_r30.0.4-windows.zip(大約12M)
接著解壓文件到指定目錄
解壓后的路徑與文件,接著為工具目錄添加系統(tǒng)環(huán)境變量
驗(yàn)證安裝結(jié)果
#執(zhí)行命令 adb version
驗(yàn)證安裝結(jié)果
手機(jī)連接電腦USB后執(zhí)行adb devices 查看手機(jī)連接狀態(tài)
查詢已連接設(shè)備/模擬器:adb devices
此處連接手機(jī),需要手機(jī)在開發(fā)者模式開啟USB調(diào)試功能。順便也開啟模擬按鍵功能,后面會(huì)用到。
該命令經(jīng)常出現(xiàn)以下問題:
offline —— 表示設(shè)備未連接成功或無響應(yīng);
device —— 設(shè)備已連接;
no device —— 沒有設(shè)備/模擬器連接;
List of devices attached 設(shè)備/模擬器未連接到 adb 或無響應(yīng)
簡單獲取手機(jī)屏幕坐標(biāo)的方法
方法1、進(jìn)入手機(jī)的開發(fā)者模式,打開在手機(jī)上實(shí)時(shí)顯示坐標(biāo)的功能,長按屏幕位置自動(dòng)顯示坐標(biāo)
方法2、使用android adb shell命令獲取
# 截屏到手機(jī) adb shell screencap /sdcard/screen.png # 將手機(jī)上剛才的截圖上傳到電腦 adb pull /sdcard/screen.png /Users/Administrator/Desktop/screen.png
從原始圖片上,使用PS扣出自己想要的局部圖片,然后使用下面的代碼獲取局部圖片在原始圖片上的坐標(biāo)
不會(huì)使用PS,可以直接使用截圖在原始圖片上截圖,但是沒PS精細(xì)。
import aircv as ac # 根據(jù)圖片在圖片上查找坐標(biāo) # imgsrc=原始圖像,imgobj=待查找的圖片,confidence=設(shè)置匹配系數(shù) def matchImg(imgsrc, imgobj, confidence=0.2): imsrc = ac.imread(imgsrc) imobj = ac.imread(imgobj) match_result = ac.find_all_template(imsrc, imobj, confidence) return match_result if __name__ == '__main__': p = matchImg("C:\\Users\\Administrator\\Desktop\\screen.png", "C:\\Users\\Administrator\\Desktop\\daicha.png") print(p)
使用python操控手機(jī)
import os import time def execute(cmd): adbstr = 'adb shell {}'.format(cmd) print(adbstr) os.system(adbstr) if __name__ == '__main__': while True: # 點(diǎn)擊位置一 execute("input tap 350 2200") time.sleep(3) # 點(diǎn)擊位置二 execute("input tap 970 135") time.sleep(5)
android adb shell 常用命令
android adb shell官方命令(英文)https://adbshell.com/
以下命令來源:
1.模擬點(diǎn)擊
adb shell input tap 100 100
2.滑動(dòng)
adb shell input swipe x1 y1 x2 y2 adb input touchscreen swipe x1 y1 x2 y2 100 adb shell input swipe 100 100 400 100 300 #左往右 adb shell input swipe 400 100 100 100 300 #右往左 adb shell input swipe 100 100 100 400 300 #上往下 adb shell input swipe 100 400 100 100 300 #下往上 adb shell input swipe 100 100 400 400 300 #上往下斜 adb shell input swipe 400 400 100 100 300 #下往上斜
3.長按
adb shell input swipe 100 100 100 100 1000 //在 100 100 位置長按 1000毫秒 adb shell input swipe 367 469 367 469 800
4.打印所有包名
adb shell pm list packages ➜ ~ adb shell pm list packages package:com.huawei.floatMms package:com.android.defcontainer package:com.tencent.mm
5.打印制定包的apk路徑
adb shell pm path com.android.phone ➜ ~ adb shell pm path com.huawei.android.launcher package:/system/app/HwLauncher6.apk
6.刪除制定包
adb shell pm clear com.test.abc
7.截圖
adb shell screencap /sdcard/screen.png adb pull /sdcard/screen.png #下載到本地
8.獲取被點(diǎn)擊的位置信息
adb shell getevent > /dev/input/event0 3 39 3e1 /dev/input/event0 1 14a 1 /dev/input/event0 1 145 1 /dev/input/event0 3 35 406 //x坐標(biāo) /dev/input/event0 3 54 1083 //y坐標(biāo) /dev/input/event0 0 0 0 /dev/input/event0 3 39 ffffffff /dev/input/event0 1 14a 0 /dev/input/event0 1 145 0 /dev/input/event0 0 0 getevent -l -c 16 輸出所有event設(shè)備的基本信息 add device 1: /dev/input/event2 name: "hi6421_on" could not get driver version for /dev/input/mouse0, Not a typewriter add device 2: /dev/input/event4 name: "huawei,touchscreen" add device 3: /dev/input/event0 name: "mhl_rcp_dev" could not get driver version for /dev/input/mice, Not a typewriter add device 4: /dev/input/event1 name: "hisi_gpio_key.14" add device 5: /dev/input/event3 name: "hi3630_hi6401_CARD Headset Jack" getevent -c 10 //輸出10條信息后退出 getevent -l //將type、code、value以對應(yīng)的常量名稱顯示
9.打開對應(yīng)的activity
adb shell am start -n {包(package)名}/{包名}.{活動(dòng)(activity)名稱} adb shell am start com.songheng.eastnews/com.oa.eastfirst.activity.WelcomeActivity
10.獲得當(dāng)前活動(dòng)窗口的信息,包名以及活動(dòng)窗體
adb shell dumpsys window windows | grep mCurrent
11.包名管理命令,獲得對應(yīng)包名的對應(yīng)apk路徑
adb shell pm path com.migu.lobby
12.使用dumpsys命令可以查看Android手機(jī)當(dāng)前正在運(yùn)行的Activity
adb shell dumpsys activity activities | findstr "Run"
13.使用 uiautomator dump 獲取app上的頁面元素
adb shell uiautomator dump /data/local/tmp/uidump.xml adb shell uiautomator dump /sdcard/dump.xml
14.下載文件
adb pull /sdcard/demo.mp4
15.上傳文件
adb push test.apk /sdcard
16.息屏
adb shell input keyevent 26
17.keyevent
adb shell input keyevent 20 #向下 adb shell input keyevent 4 #返回 adb shell input keyevent 3 #Home adb shell input keyevent 6 #掛機(jī) adb shell input keyevent 84 #搜索 adb shell input keyevent 26 #電源 adb shell input keyevent 24 #音量+ adb shell input keyevent 25 #音量-
18.輸入框輸入
adb shell input text "ANDROID"
19.利用無線來查看adb shell
> adb tcpip 5555 連接: > adb connect IP:5555
20.查看所有已經(jīng)連接上的設(shè)備
adb devices
21.安裝卸載
adb install <apk文件路徑> adb install -r <apk文件路徑> 通過install命令來安裝apk文件,-r參數(shù)可以重新安裝某個(gè)應(yīng)用并保留應(yīng)用數(shù)據(jù) adb install -r ~/chrome.apk 卸載應(yīng)用: adb uninstall <軟件名> adb uninstall -k < 軟件名> 如果加 -k 參數(shù),為卸載軟件但是保留配置和緩存文件 adb uninstall com.android.chrome
22.關(guān)機(jī)命令
adb shell su reboot -p
到此這篇關(guān)于python實(shí)現(xiàn)電腦操控安卓手機(jī)的文章就介紹到這了,更多相關(guān)python電腦操控手機(jī)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python通過裝飾器檢查函數(shù)參數(shù)數(shù)據(jù)類型的方法
這篇文章主要介紹了python通過裝飾器檢查函數(shù)參數(shù)數(shù)據(jù)類型的方法,涉及Python裝飾器的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03Python xlrd excel文件操作代碼實(shí)例
這篇文章主要介紹了Python xlrd excel文件操作代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Python 給下載文件顯示進(jìn)度條和下載時(shí)間的實(shí)現(xiàn)
這篇文章主要介紹了Python 給下載文件顯示進(jìn)度條和下載時(shí)間的代碼,本文通過實(shí)例代碼截圖相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Pytorch深度學(xué)習(xí)gather一些使用問題解決方案
這篇文章主要為大家介紹了Pytorch深度學(xué)習(xí),在使用gather過程中遇到的一下問題,下面給出解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09python實(shí)現(xiàn)遠(yuǎn)程控制電腦
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)遠(yuǎn)程控制電腦,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05教你用python實(shí)現(xiàn)一個(gè)加密的文字處理器
生活中有時(shí)候我們需要對一些重要的文件進(jìn)行加密,下面這篇文章主要給大家介紹了關(guān)于如何用python實(shí)現(xiàn)一個(gè)加密文字處理器的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06