Home  >  Article  >  Backend Development  >  使用python编写android截屏脚本双击运行即可

使用python编写android截屏脚本双击运行即可

WBOY
WBOYOriginal
2016-06-16 08:43:091125browse

测试的过程中经常需要截取屏幕,通常的做法是使用手机自带的截屏功能,然后将截屏文件复制出来,这种方法的优点是不需要连接数据线就可截屏,缺点则是生成的截屏文件命名是随机命名的,复制出来也比较麻烦。另一种方法是使用PC端的手机助手类软件。

这里使用python编写一个截屏的脚本,双击运行脚本就OK,截屏成功后会将截屏文件已当前时间命名,并保存在存放脚本的当前路径的screenshot文件夹下:

#!/usr/bin/env python 

import os 
import time 

PATH = lambda p: os.path.abspath(p) 

def screenshot(): 
path = PATH(os.getcwd() + "/screenshot") 
timestamp = time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time())) 
os.popen("adb wait-for-device") 
os.popen("adb shell screencap -p /data/local/tmp/tmp.png") 
if not os.path.isdir(PATH(os.getcwd() + "/screenshot")): 
os.makedirs(path) 
os.popen("adb pull /data/local/tmp/tmp.png " + PATH(path + "/" + timestamp + ".png")) 
os.popen("adb shell rm /data/local/tmp/tmp.png") 
print "success" 

if __name__ == "__main__": 
screenshot()
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn