手机运行python控制手机app,python模拟手机屏幕点击 | 您所在的位置:网站首页 › python控制android模拟器 › 手机运行python控制手机app,python模拟手机屏幕点击 |
一、电脑下载并安装SDK Platform Tools
手机连接电脑USB后执行adb devices 查看手机连接状态 查询已连接设备/模拟器:adb devices 此处连接手机,需要手机在开发者模式开启USB调试功能。顺便也开启模拟按键功能,后面会用到python画六瓣花。 该命令经常出现以下问题: offline —— 表示设备未连接成功或无响应; device —— 设备已连接; no device —— 没有设备/模拟器连接; List of devices attached 设备/模拟器未连接到 adb 或无响应 简单获取手机屏幕坐标的方法方法1、进入手机的开发者模式,打开在手机上实时显示坐标的功能,长按屏幕位置自动显示坐标 方法2、使用android adb shell命令获取 # 截屏到手机 adb shell screencap /sdcard/screen.png # 将手机上刚才的截图上传到电脑 adb pull /sdcard/screen.png /Users/Administrator/Desktop/screen.png从原始图片上,使用PS扣出自己想要的局部图片,然后使用下面的代码获取局部图片在原始图片上的坐标 不会使用PS,可以直接使用截图在原始图片上截图,但是没PS精细。 import aircv as ac # 根据图片在图片上查找坐标 # imgsrc=原始图像,imgobj=待查找的图片,confidence=设置匹配系数 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) adb模拟按键操作1.模拟文本输入操作 ## 模拟输入abc adb shell input text abc2.通过键值模拟按键操作 ## 模拟按back键 adb shell input keyevent 4 ## 或者 adb shell input keyevent KEYCODE_BACK3.通过坐标模拟点击操作 ## 点击横坐标300,纵坐标500的点 adb shell input tap 300 5004.通过坐标模拟滑动操作 ## 从(200,300)的点划到(500,300)的点,滑动时间100ms adb shell input swipe 200 300 500 300 1005.通过坐标模拟拖动操作 ## 从(100,1220)的点拖动到(500,620)的点,滑动时间2000ms adb shell input draganddrop 100 1220 500 620 20006.通过坐标模拟长按滑动操作(注意与滑动操作的区别) 按住不放,从(200,300)的点划到(201,301)的点,滑动时间2000ms,由于划动距离短,时间长,在一个图标之内则主观显示为长按操作 可以将游戏背包的物品拖动,扔到仓库。 adb shell input swipe 200 300 201 301 20007.模拟实体键长按操作 通过 getevent & 获取实体键键值 /dev/input/event4: 0001 0074 00000001 /dev/input/event4: 0000 0000 00000000 /dev/input/event4: 0001 0074 00000000 /dev/input/event4: 0000 0000 000000000074为电源键键值,转换为十进制为116,编写如下脚本 sendevent /dev/input/event4 1 116 1 sendevent /dev/input/event4 0 0 0 sleep 4 sendevent /dev/input/event4 1 116 0 sendevent /dev/input/event4 0 0 08.通过坐标/键值模拟同时按下两个键,用and连接 ## 电源键亮屏并滑动解锁 adb shell input keyevent KEYCODE_POWER and KEYCODE_MENU android adb shell 常用命令android adb shell官方命令(英文)https://adbshell.com/ 以下命令来源:https://www.jianshu.com/p/ccd35afa8470 1.模拟点击 adb shell input tap 100 1002.滑动 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 8004.打印所有包名 adb shell pm list packages ➜ ~ adb shell pm list packages package:com.huawei.floatMms package:com.android.defcontainer package:com.tencent.mm5.打印制定包的apk路径 adb shell pm path com.android.phone ➜ ~ adb shell pm path com.huawei.android.launcher package:/system/app/HwLauncher6.apk6.删除制定包 adb shell pm clear com.test.abc7.截图 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坐标 /dev/input/event0 3 54 1083 //y坐标 /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设备的基本信息 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以对应的常量名称显示9.打开对应的activity adb shell am start -n {包(package)名}/{包名}.{活动(activity)名称} adb shell am start com.songheng.eastnews/com.oa.eastfirst.activity.WelcomeActivity10.获得当前活动窗口的信息,包名以及活动窗体 adb shell dumpsys window windows | grep mCurrent11.包名管理命令,获得对应包名的对应apk路径 adb shell pm path com.migu.lobby12.使用dumpsys命令可以查看Android手机当前正在运行的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.xml14.下载文件 adb pull /sdcard/demo.mp415.上传文件 adb push test.apk /sdcard16.息屏 adb shell input keyevent 2617.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:555520.查看所有已经连接上的设备 adb devices21.安装卸载 adb install adb install -r 通过install命令来安装apk文件,-r参数可以重新安装某个应用并保留应用数据 adb install -r ~/chrome.apk 卸载应用: adb uninstall adb uninstall -k < 软件名> 如果加 -k 参数,为卸载软件但是保留配置和缓存文件 adb uninstall com.android.chrome22.关机命令 adb shell su reboot -p |
CopyRight 2018-2019 实验室设备网 版权所有 |