Use python to write an android screenshot script that can be double clicked to run

  • 2020-04-02 13:53:09
  • OfStack

In the process of testing, it is often necessary to intercept the screen. The usual method is to use the screenshot function of the phone and then copy the screenshot file. The advantage of this method is that it does not need to connect the data line to take a screenshot, but the disadvantage is that the generated screenshot file is randomly named, so it is more difficult to copy out. Another way is to use mobile assistant software on the PC side.

Here, use python to write a screenshot script. Double-click to run the script, and it will be OK. After successful screenshot, the screenshot file will be named after the current time and saved in the screenshot folder of the current path of the script:


#!/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()

Related articles: