App下載

python怎么使用電腦操控安卓手機?

猿友 2021-07-20 14:27:34 瀏覽數(shù) (3412)
反饋

隨著智能手機的市場份額超過PC成為個人微機的大頭,更多的應(yīng)用的開發(fā)會更傾向于安卓。安卓應(yīng)用以點按和拖動為主,輸入也通過觸摸屏實現(xiàn),這樣的應(yīng)用的功能測試也是通過點按和拖動為主的。然而pc端和瀏覽器有優(yōu)秀的自動化的功能測試工具,而手機端卻沒有比較合適的類似工具。實際上安卓提供了一個AndroidSDK,里面提供了與安卓平臺交互的工具(比如adb shell),這樣就實現(xiàn)了電腦與手機的交互。接下來小編就教你如何在adbshell介入的方式下使用python語言來控制電腦操作手機吧。

一、電腦下載并安裝SDK Platform Tools


安裝AndroidSDK工具包


SDK下載

下載后的文件:platform-tools_r30.0.4-windows.zip(大約12M)

接著解壓文件到指定目錄


SDK文件夾

解壓后的路徑與文件,接著為工具目錄添加系統(tǒng)環(huán)境變量

配置環(huán)境變量

驗證安裝結(jié)果

#執(zhí)行命令
adb version

命令行

驗證安裝結(jié)果

手機連接電腦USB后執(zhí)行adb devices 查看手機連接狀態(tài)

查詢已連接設(shè)備/模擬器:adb devices

此處連接手機,需要手機在開發(fā)者模式開啟USB調(diào)試功能。順便也開啟模擬按鍵功能,后面會用到。

該命令經(jīng)常出現(xiàn)以下問題:

offline —— 表示設(shè)備未連接成功或無響應(yīng);

device —— 設(shè)備已連接;

no device —— 沒有設(shè)備/模擬器連接;

List of devices attached 設(shè)備/模擬器未連接到 adb 或無響應(yīng)

簡單獲取手機屏幕坐標(biāo)的方法

方法1、進入手機的開發(fā)者模式,打開在手機上實時顯示坐標(biāo)的功能,長按屏幕位置自動顯示坐標(biāo)

方法2、使用android adb shell命令獲取

# 截屏到手機
adb shell screencap /sdcard/screen.png
# 將手機上剛才的截圖上傳到電腦
adb pull /sdcard/screen.png  /Users/Administrator/Desktop/screen.png

從原始圖片上,使用PS扣出自己想要的局部圖片,然后使用下面的代碼獲取局部圖片在原始圖片上的坐標(biāo)
不會使用PS,可以直接使用截圖在原始圖片上截圖,但是沒PS精細。

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操控手機

import os
import time


def execute(cmd):
    adbstr = 'adb shell {}'.format(cmd)
    print(adbstr)
    os.system(adbstr)


if __name__ == '__main__':

    while True:
        # 點擊位置一
        execute("input tap 350 2200")
        time.sleep(3)
      
        # 點擊位置二
        execute("input tap 970 135")
        time.sleep(5)

android adb shell 常用命令

android adb shell官方命令(英文)https://adbshell.com/

以下命令來源:

1.模擬點擊

adb shell input tap 100 100

2.滑動

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.獲取被點擊的位置信息

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)名}/{包名}.{活動(activity)名稱}

adb shell am start com.songheng.eastnews/com.oa.eastfirst.activity.WelcomeActivity

10.獲得當(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手機當(dāng)前正在運行的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 #掛機

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ù)可以重新安裝某個應(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)機命令

adb shell
su
reboot -p

小結(jié)

到此這篇關(guān)于python電腦操控手機的文章就介紹到這了,更多python學(xué)習(xí)內(nèi)容請搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章。

0 人點贊