diff --git a/playwright_automation.py b/playwright_automation.py index d2f28c1..de93e0c 100755 --- a/playwright_automation.py +++ b/playwright_automation.py @@ -1375,24 +1375,13 @@ class PlaywrightAutomation: # 先保存到临时文件 temp_filepath = filepath + '.tmp' - # 获取iframe并截取其完整内容 - iframe = self.get_iframe_safe() - if iframe: - # 对iframe内容进行全页截图 - iframe.screenshot( - path=temp_filepath, - type='jpeg', - full_page=True, - quality=100 - ) - else: - # 回退到主页面截图 - self.main_page.screenshot( - path=temp_filepath, - type='jpeg', - full_page=True, - quality=100 - ) + # 截图:默认截取主页面(包含左侧菜单/顶部栏等整体界面) + self.main_page.screenshot( + path=temp_filepath, + type='jpeg', + full_page=True, + quality=100 + ) # 验证文件是否成功创建且大小合理 if not os.path.exists(temp_filepath): diff --git a/services/screenshots.py b/services/screenshots.py index a4e4c3a..2a58698 100644 --- a/services/screenshots.py +++ b/services/screenshots.py @@ -117,18 +117,40 @@ def take_screenshot_for_account( else: bz = 2 # 应读 target_url = f"{base}/admin/center.aspx?bz={bz}" - automation.main_page.goto(target_url, timeout=60000) - current_url = getattr(automation.main_page, "url", "") or "" - if "center.aspx" not in current_url: - raise RuntimeError(f"unexpected_url:{current_url}") + # 目标:保留外层框架(左侧菜单/顶部栏),仅在 mainframe 内部导航到目标内容页 + iframe = None try: - automation.main_page.wait_for_load_state("networkidle", timeout=30000) + iframe = automation.get_iframe_safe(retry=True, max_retries=5) except Exception: - pass - try: - automation.main_page.wait_for_selector("table.ltable", timeout=20000) - except Exception: - pass + iframe = None + + if iframe: + iframe.goto(target_url, timeout=60000) + current_url = getattr(iframe, "url", "") or "" + if "center.aspx" not in current_url: + raise RuntimeError(f"unexpected_iframe_url:{current_url}") + try: + iframe.wait_for_load_state("networkidle", timeout=30000) + except Exception: + pass + try: + iframe.wait_for_selector("table.ltable", timeout=20000) + except Exception: + pass + else: + # 兜底:若获取不到 iframe,则退回到主页面直达 + automation.main_page.goto(target_url, timeout=60000) + current_url = getattr(automation.main_page, "url", "") or "" + if "center.aspx" not in current_url: + raise RuntimeError(f"unexpected_url:{current_url}") + try: + automation.main_page.wait_for_load_state("networkidle", timeout=30000) + except Exception: + pass + try: + automation.main_page.wait_for_selector("table.ltable", timeout=20000) + except Exception: + pass navigated = True except Exception as nav_error: log_to_client(f"直达页面失败,将尝试按钮切换: {str(nav_error)[:120]}", user_id, account_id) @@ -269,4 +291,3 @@ def take_screenshot_for_account( ) if not submitted: screenshot_callback(None, "截图队列已满,请稍后重试") -