commit
This commit is contained in:
@@ -38,6 +38,20 @@ async def save_error_screenshot(
|
||||
return None
|
||||
|
||||
try:
|
||||
# 检查页面状态
|
||||
try:
|
||||
current_url = page.url
|
||||
print(f"[错误截图] 当前URL: {current_url}", file=sys.stderr)
|
||||
|
||||
# 检查是否是空白页
|
||||
if current_url in ['about:blank', '', 'data:,']:
|
||||
print(f"[错误截图] 警告: 当前页面为空白页,截图可能没有内容", file=sys.stderr)
|
||||
|
||||
# 等待页面稳定
|
||||
await page.wait_for_load_state('domcontentloaded', timeout=3000)
|
||||
except Exception as state_error:
|
||||
print(f"[错误截图] 检查页面状态失败: {str(state_error)}", file=sys.stderr)
|
||||
|
||||
# 生成文件名:年月日时分秒_错误类型.png
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
|
||||
@@ -52,11 +66,21 @@ async def save_error_screenshot(
|
||||
|
||||
filepath = SCREENSHOT_DIR / filename
|
||||
|
||||
# 截图
|
||||
await page.screenshot(path=str(filepath), full_page=True)
|
||||
# 截图(添加超时和全页截图)
|
||||
await page.screenshot(
|
||||
path=str(filepath),
|
||||
full_page=True,
|
||||
timeout=10000 # 10秒超时
|
||||
)
|
||||
|
||||
# 检查截图文件大小
|
||||
file_size = filepath.stat().st_size
|
||||
print(f"[错误截图] 已保存: {filepath} (大小: {file_size} bytes)", file=sys.stderr)
|
||||
|
||||
# 如果文件太小(小于5KB),可能是空白截图
|
||||
if file_size < 5120:
|
||||
print(f"[错误截图] 警告: 截图文件过小 ({file_size} bytes),可能为空白页面", file=sys.stderr)
|
||||
|
||||
# 打印日志
|
||||
print(f"[错误截图] 已保存: {filepath}", file=sys.stderr)
|
||||
if error_message:
|
||||
print(f"[错误截图] 错误信息: {error_message}", file=sys.stderr)
|
||||
|
||||
@@ -115,6 +139,19 @@ async def save_screenshot_with_html(
|
||||
return None, None
|
||||
|
||||
try:
|
||||
# 检查页面状态
|
||||
try:
|
||||
current_url = page.url
|
||||
print(f"[错误截图] 当前URL: {current_url}", file=sys.stderr)
|
||||
|
||||
if current_url in ['about:blank', '', 'data:,']:
|
||||
print(f"[错误截图] 警告: 当前页面为空白页", file=sys.stderr)
|
||||
|
||||
# 等待页面稳定
|
||||
await page.wait_for_load_state('domcontentloaded', timeout=3000)
|
||||
except Exception as state_error:
|
||||
print(f"[错误截图] 检查页面状态失败: {str(state_error)}", file=sys.stderr)
|
||||
|
||||
# 生成文件名
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
safe_error_type = "".join(c for c in error_type if c.isalnum() or c in ('_', '-'))
|
||||
@@ -126,7 +163,16 @@ async def save_screenshot_with_html(
|
||||
|
||||
# 保存截图
|
||||
screenshot_path = SCREENSHOT_DIR / f"{base_filename}.png"
|
||||
await page.screenshot(path=str(screenshot_path), full_page=True)
|
||||
await page.screenshot(
|
||||
path=str(screenshot_path),
|
||||
full_page=True,
|
||||
timeout=10000
|
||||
)
|
||||
|
||||
# 检查截图文件大小
|
||||
screenshot_size = screenshot_path.stat().st_size
|
||||
if screenshot_size < 5120:
|
||||
print(f"[错误截图] 警告: 截图文件过小 ({screenshot_size} bytes)", file=sys.stderr)
|
||||
|
||||
# 保存HTML
|
||||
html_path = SCREENSHOT_DIR / f"{base_filename}.html"
|
||||
@@ -134,8 +180,9 @@ async def save_screenshot_with_html(
|
||||
with open(html_path, 'w', encoding='utf-8') as f:
|
||||
f.write(html_content)
|
||||
|
||||
print(f"[错误截图] 已保存截图: {screenshot_path}", file=sys.stderr)
|
||||
print(f"[错误截图] 已保存HTML: {html_path}", file=sys.stderr)
|
||||
html_size = html_path.stat().st_size
|
||||
print(f"[错误截图] 已保存截图: {screenshot_path} ({screenshot_size} bytes)", file=sys.stderr)
|
||||
print(f"[错误截图] 已保存HTML: {html_path} ({html_size} bytes)", file=sys.stderr)
|
||||
if error_message:
|
||||
print(f"[错误截图] 错误信息: {error_message}", file=sys.stderr)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user