Python实现对Android截图
背景:
测试过程中,总是需要对Android设备进行截图,然后在截图中标注问题描述;
手动方式:
1.使用adb scrrencap /sdcard/screen.png 命令对Android设备进行截图
2.然后再使用adb pull /sdcard/scrren.png导入到PC端
3.使用QQ截图进行问题描述标注
自动化实现:
将scrrencap.py文件copy至某个目录下,直接执行将保存截图到当前目录并自动打开展示;
C:\>screencap.py
使用方法:
C:\>screencap.py -h Usage: screencap.py [-d <directory> -f <filename>] Automatic screenshots for android, After in PC display . Options: -h, --help show this help message and exit -d DIRECTORY, --dir=DIRECTORY directory of save the address -f FILENAME, --filename=FILENAME filename of screen shots file name
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import os 2 import time 3 from optparse import OptionParser 4 5 6 def option(): 7 # 获取脚本所在当前目录 8 current_dir = os.path.dirname(__file__) 9 # 根据截图时间生成默认文件名:20170722142831.png 10 file_name = "%s.png" % time.strftime("%Y%m%d%H%M%S", time.localtime()) 11 12 usage = "screencap.py [-d <directory> -f <filename>]" 13 description = "Automatic screenshots for android, After in PC display ." 14 15 p = OptionParser(usage=usage, description=description) 16 17 p.add_option("-d", "--dir", 18 dest="directory", default=current_dir, 19 help="directory of save the address") 20 21 p.add_option("-f", "--filename", 22 dest="filename", default=file_name, 23 help="filename of screen shots file name") 24 return p.parse_args() 25 26 27 def screen(options): 28 # 截图 29 print(os.popen("adb shell screencap /sdcard/{filename}".format(filename=options.filename)).read()) 30 31 # 截图导出 32 print(os.popen(r"adb pull /sdcard/{filename} {dir}".format(filename=options.filename, 33 dir=options.directory)).read()) 34 # 打开截图 35 print(os.popen(r"start {filename}".format(filename=options.filename)).read()) 36 # 删除截图 37 print(os.popen("adb shell rm /sdcard/{filename}".format(filename=options.filename))) 38 39 40 if __name__ == '__main__': 41 options, args = option() 42 # print(options) 43 # print(args) 44 screen(options)