程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python+adb screenshot, screen recording, log capture

編輯:Python

brief introduction : In some special situations , Passed adb Connect , Then you can take a screenshot , Recording screen , Crawl log operation .

Related strategies :

adb:5 Seconds to finish viewing App Package name and startup page Activity

adb Restart or shut down the phone

adb: The android mobile phone USB Debug mode

adb:win10 Installation under system

adb: Common commands

Case study 1: Screenshot

# Screenshot 
adb shell screencap /sdcard/202206280928.png
# Pull screenshot 
adb pull sdcard/202206280928.png ./

Case study 2: Recording screen , Press Ctrl+C Stop recording

# Recording screen 
adb shell screenrecord /sdcard/202206280932.mp4
# Pull screenshot 
adb pull /sdcard/202206280932.mp4 ./

Case study 3: Grab logs , Press Ctrl+C Stop grabbing

adb logcat -v time >.\log"%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%-%time:~3,2%-%time:~6,2%.txt"

Transform into Python Script form :

# -*- coding: utf-8 -*-
# time: 2022/6/28 10:18
# file: adb_test.py
# author: tom
# official account : Play with test development 
import os
import time
import datetime
import subprocess
import threading
class ADB:
def __init__(self, device, target_path):
self.target_path = target_path
subprocess.Popen(f"adb connect {
device}")
def screenshot(self):
# Use && separate .
file_name = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
step1 = rf"cd {
self.target_path}"
step2 = f"adb shell screencap /sdcard/{
file_name}.png"
step3 = f"adb pull sdcard/{
file_name}.png ./"
command = fr"{
step1} && {
step2} && {
step3}"
os.popen(command)
def video(self, record_time):
# collection 
file_name = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
step1 = rf"cd {
self.target_path}"
step2 = f"adb shell screenrecord --time-limit {
record_time} /sdcard/{
file_name}.mp4"
command = fr"{
step1} && {
step2}"
os.system(command)
# Pull 
step4 = rf"adb pull sdcard/{
file_name}.mp4 ./"
command2 = fr"{
step1} && {
step4}"
os.system(command2)
if __name__ == '__main__':
device = "192.168.xxx.xxx"
target_path = r"C:\adbTest\test"
adb = ADB(device, target_path)
adb.screenshot()
adb.video(10)

WeChat official account : Play with test development
Welcome to your attention , Common progress , thank you !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved