From 94ceb959c72f5e5a94c3d4589cf91fe863c954f7 Mon Sep 17 00:00:00 2001 From: yuyx <237899745@qq.com> Date: Sun, 14 Dec 2025 16:11:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BD=BF=E7=94=A8JavaScript=E5=B1=95?= =?UTF-8?q?=E5=BC=80iframe=E9=AB=98=E5=BA=A6=E4=BB=A5=E6=88=AA=E5=8F=96?= =?UTF-8?q?=E5=AE=8C=E6=95=B4=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在截图前通过JavaScript动态调整iframe及其父容器的高度,使其能够显示全部内容。 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- playwright_automation.py | 74 ++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/playwright_automation.py b/playwright_automation.py index 83460a5..df4375d 100755 --- a/playwright_automation.py +++ b/playwright_automation.py @@ -1361,43 +1361,43 @@ class PlaywrightAutomation: # 先保存到临时文件 temp_filepath = filepath + '.tmp' - # 获取iframe - iframe = self.get_iframe_safe() - if iframe: - # 尝试截取iframe内的主要内容区域 - try: - # 查找内容表格 - table_locator = iframe.locator("table.ltable") - if table_locator.count() > 0: - table_locator.first.screenshot( - path=temp_filepath, - type='jpeg', - quality=100 - ) - else: - # 如果没有表格,截取整个body内容 - iframe.locator("body").screenshot( - path=temp_filepath, - type='jpeg', - quality=100 - ) - except Exception as e: - self.log(f"元素截图失败: {e},尝试全页截图") - # 回退到iframe全页截图 - iframe.screenshot( - path=temp_filepath, - type='jpeg', - full_page=True, - quality=100 - ) - else: - # 如果无法获取iframe,回退到主页面截图 - self.main_page.screenshot( - path=temp_filepath, - type='jpeg', - full_page=True, - quality=100 - ) + # 尝试展开iframe以显示全部内容 + try: + self.main_page.evaluate("""() => { + const iframe = document.querySelector('iframe[name="mainframe"]'); + if (iframe) { + try { + const iframeDoc = iframe.contentDocument || iframe.contentWindow.document; + const contentHeight = Math.max( + iframeDoc.body.scrollHeight, + iframeDoc.documentElement.scrollHeight, + 2000 + ); + iframe.style.height = contentHeight + 'px'; + iframe.style.minHeight = contentHeight + 'px'; + iframe.style.maxHeight = 'none'; + // 调整父容器 + let parent = iframe.parentElement; + while (parent && parent !== document.body) { + parent.style.height = 'auto'; + parent.style.maxHeight = 'none'; + parent.style.overflow = 'visible'; + parent = parent.parentElement; + } + } catch(e) {} + } + }""") + time.sleep(0.5) + except Exception: + pass + + # 截取整个页面 + self.main_page.screenshot( + path=temp_filepath, + type='jpeg', + full_page=True, + quality=100 + ) # 验证文件是否成功创建且大小合理 if not os.path.exists(temp_filepath):