目录
连接
常用操作
截图到PC端
使用pure-python-adb库可以实现python控制手机,支持input输入,支持shell命令,支持pull/push上传下载文件等。
安装库:pip install pure-python-adb
电脑端需要安装adb程序,在命令行窗口启动adb程序,如下:
连接
from ppadb.client import Client as adbct
client = adbct(host='127.0.0.1', port=5037) # 创建连接
devices = client.devices() # 连接设备
print(len(devices), devices) # 打印连接设备数量和连接对象
if len(devices) != 0:
device = devices[0] # 获取第一个连接对象
常用操作
print(device.cpu_times()) # 获取CPU信息
print(device.shell('getprop ro.product.model')) # 获取手机名称
print(device.shell('getprop ro.build.version.release')) # 获取手机版本
print(device.shell('getprop ro.product.brand')) # 获取手机厂商
print(device.wm_size()) # 获取屏幕分辨率
device.input_swipe(800, 2000, 800, 300, 500) # 滑动
device.input_text('123456') # 输入内容
device.input_tap(300, 500) # 点击
device.input_keyevent('26') # 点击电源键
截图到PC端
screenshort = device.screencap()
with open('手机截屏.png', 'wb') as f:
f.write(screenshort)
from ppadb.client import Client as adbct
client = adbct(host='127.0.0.1', port=5037) # 创建连接
devices = client.devices() # 连接设备
if len(devices) != 0:
print('当前有{}台设备'.format(len(devices)))
ds = [devices[i] for i in range((len(devices)))]
dl = list(enumerate(ds))
for d in dl:
print('设备{}为{}'.format(d[0], d[1].shell('getprop ro.product.model')))
else:
quit()
index = eval(input('输入要操作的设备设备序号:'))
device = devices[index]
while 1:
t = input('<<回车继续截图,输入任意字符回车退出截图:')
if not t:
screenshort = device.screencap()
with open(f'手机截屏{int(time.time() * 100000)}.png', 'wb') as f:
f.write(screenshort)
print('已截图保存')
else:
break