# -*- coding: utf-8 -*- """验证 Edge 无头模式是不是把窗口宽度夹住了。 怀疑:--window-size=420 实际渲染宽度比 420 大(Edge 有最小窗口宽度), 截图再裁到 420 —— 于是看起来像"右边被截断", 其实页面本身没问题。 做法:造一个宽度=100% 的蓝色条,看截图里蓝条有多宽。 如果蓝条比截图宽,就说明渲染宽度 > 截图宽度(被裁了)。 """ from __future__ import annotations import subprocess import sys import tempfile from pathlib import Path EDGE = next((p for p in ( Path(r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"), Path(r"C:\Program Files\Microsoft\Edge\Application\msedge.exe")) if p.is_file()), None) def main() -> int: if not EDGE: print(" 没找到 Edge") return 1 from PIL import Image work = Path(tempfile.mkdtemp(prefix="edge_probe_")) page = work / "probe.html" page.write_text( '' '
' "", encoding="utf-8") print(f" {'请求宽度':>8} {'截图宽度':>8} {'蓝条宽度':>8} {'判断':>14}") for width in (400, 420, 480, 500, 560, 600, 700, 800): shot = work / f"s{width}.png" subprocess.run( [str(EDGE), "--headless=new", "--disable-gpu", "--hide-scrollbars", f"--window-size={width},300", f"--screenshot={shot}", page.as_uri()], capture_output=True, timeout=60) if not shot.is_file(): print(f" {width:>8} 截图失败") continue img = Image.open(shot).convert("RGB") row = [img.getpixel((x, 20)) for x in range(img.width)] blue = sum(1 for c in row if c[2] > 180 and c[0] < 100) verdict = "正常" if abs(blue - img.width) <= 2 else "被裁了!" print(f" {width:>8} {img.width:>8} {blue:>8} {verdict:>14}") print() print(" 结论:如果某些宽度显示[被裁了],说明 Edge 无头版有最小窗口宽度,") print(" 截图比实际渲染窄 —— 那么窄屏截图看起来的截断是假象。") return 0 if __name__ == "__main__": sys.exit(main())