1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
from selenium import webdriver
import shutil
import time, datetime
import os
import functools
from workpath import workdir
# 截图装饰器
def screenshot_on_failure(func):
@functools.wraps(func)
def warpper(self, *args, **kwargs):
try:
return func(self, *args, **kwargs)
except:
driver = self.driver
if driver:
formatted_time = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d-%H-%M-%S')
file = "{}.png".format(formatted_time)
file_path = os.path.join(workdir, "results", "screenshot", file)
file_path_work = os.path.join(workdir, "results", "screenshot")
if not os.path.exists(file_path_work):
os.makedirs(file_path_work)
if not os.path.exists(file_path):
#print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], f"截图保存于:{file_path}")
driver.save_screenshot(file_path)
#删除历史7天前数据
try:
clear_history(file_path)
except:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "please check")
#print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "删除历史数据失败...")
raise
return warpper
def clear_history(file_path, retain_time=3600*24*1):
time_now = time.time()
file_work = os.path.dirname(file_path)
data_list = [os.path.join(file_work, i) for i in os.listdir(file_work)]
for datapath in data_list:
if time_now - os.path.getctime(datapath) > retain_time:
try:
os.remove(datapath)
except:
shutil.rmtree(datapath)
|