fix: 使用JavaScript展开iframe高度以截取完整内容

在截图前通过JavaScript动态调整iframe及其父容器的高度,使其能够显示全部内容。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-14 16:11:58 +08:00
parent b408e78c74
commit 94ceb959c7

View File

@@ -1361,43 +1361,43 @@ class PlaywrightAutomation:
# 先保存到临时文件 # 先保存到临时文件
temp_filepath = filepath + '.tmp' temp_filepath = filepath + '.tmp'
# 获取iframe # 尝试展开iframe以显示全部内容
iframe = self.get_iframe_safe() try:
if iframe: self.main_page.evaluate("""() => {
# 尝试截取iframe内的主要内容区域 const iframe = document.querySelector('iframe[name="mainframe"]');
try: if (iframe) {
# 查找内容表格 try {
table_locator = iframe.locator("table.ltable") const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
if table_locator.count() > 0: const contentHeight = Math.max(
table_locator.first.screenshot( iframeDoc.body.scrollHeight,
path=temp_filepath, iframeDoc.documentElement.scrollHeight,
type='jpeg', 2000
quality=100 );
) iframe.style.height = contentHeight + 'px';
else: iframe.style.minHeight = contentHeight + 'px';
# 如果没有表格截取整个body内容 iframe.style.maxHeight = 'none';
iframe.locator("body").screenshot( // 调整父容器
path=temp_filepath, let parent = iframe.parentElement;
type='jpeg', while (parent && parent !== document.body) {
quality=100 parent.style.height = 'auto';
) parent.style.maxHeight = 'none';
except Exception as e: parent.style.overflow = 'visible';
self.log(f"元素截图失败: {e},尝试全页截图") parent = parent.parentElement;
# 回退到iframe全页截图 }
iframe.screenshot( } catch(e) {}
path=temp_filepath, }
type='jpeg', }""")
full_page=True, time.sleep(0.5)
quality=100 except Exception:
) pass
else:
# 如果无法获取iframe回退到主页面截图 # 截取整个页面
self.main_page.screenshot( self.main_page.screenshot(
path=temp_filepath, path=temp_filepath,
type='jpeg', type='jpeg',
full_page=True, full_page=True,
quality=100 quality=100
) )
# 验证文件是否成功创建且大小合理 # 验证文件是否成功创建且大小合理
if not os.path.exists(temp_filepath): if not os.path.exists(temp_filepath):