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