当前位置:   article > 正文

python与adb无缝衔接控制手机(手机截屏实例)_python adb 控制手机

python adb 控制手机

目录

连接

常用操作

截图到PC端


使用pure-python-adb库可以实现python控制手机,支持input输入,支持shell命令,支持pull/push上传下载文件等。

安装库:pip install pure-python-adb

电脑端需要安装adb程序,在命令行窗口启动adb程序,如下:

连接

  1. from ppadb.client import Client as adbct
  2. client = adbct(host='127.0.0.1', port=5037) # 创建连接
  3. devices = client.devices() # 连接设备
  4. print(len(devices), devices) # 打印连接设备数量和连接对象
  5. if len(devices) != 0:
  6. device = devices[0] # 获取第一个连接对象

常用操作

  1. print(device.cpu_times()) # 获取CPU信息
  2. print(device.shell('getprop ro.product.model')) # 获取手机名称
  3. print(device.shell('getprop ro.build.version.release')) # 获取手机版本
  4. print(device.shell('getprop ro.product.brand')) # 获取手机厂商
  5. print(device.wm_size()) # 获取屏幕分辨率
  6. device.input_swipe(800, 2000, 800, 300, 500) # 滑动
  7. device.input_text('123456') # 输入内容
  8. device.input_tap(300, 500) # 点击
  9. device.input_keyevent('26') # 点击电源键

截图到PC端

  1. screenshort = device.screencap()
  2. with open('手机截屏.png', 'wb') as f:
  3. f.write(screenshort)
  1. from ppadb.client import Client as adbct
  2. client = adbct(host='127.0.0.1', port=5037) # 创建连接
  3. devices = client.devices() # 连接设备
  4. if len(devices) != 0:
  5. print('当前有{}台设备'.format(len(devices)))
  6. ds = [devices[i] for i in range((len(devices)))]
  7. dl = list(enumerate(ds))
  8. for d in dl:
  9. print('设备{}为{}'.format(d[0], d[1].shell('getprop ro.product.model')))
  10. else:
  11. quit()
  12. index = eval(input('输入要操作的设备设备序号:'))
  13. device = devices[index]
  14. while 1:
  15. t = input('<<回车继续截图,输入任意字符回车退出截图:')
  16. if not t:
  17. screenshort = device.screencap()
  18. with open(f'手机截屏{int(time.time() * 100000)}.png', 'wb') as f:
  19. f.write(screenshort)
  20. print('已截图保存')
  21. else:
  22. break
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/474954
推荐阅读
相关标签
  

闽ICP备14008679号