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:
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user