"""对比截图:本地手机端首页 vs 参考站首页(桌面 + 手机两套视口)""" import os import time from playwright.sync_api import sync_playwright OUT = r'D:\bwstudio\pear-admin-flask\scripts\screenshots\compare_home' os.makedirs(OUT, exist_ok=True) URLS = { 'local': 'http://192.168.1.10:5000/', 'ref': 'http://192.168.1.100/', } VIEWPORTS = { 'desktop': {'width': 1440, 'height': 900}, 'mobile': {'width': 390, 'height': 844}, # iPhone 13 } with sync_playwright() as pw: browser = pw.chromium.launch(headless=True, args=['--no-sandbox', '--disable-gpu']) for vp_name, vp in VIEWPORTS.items(): ctx = browser.new_context(viewport=vp, device_scale_factor=2, user_agent='Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15' if vp_name=='mobile' else None) page = ctx.new_page() for label, url in URLS.items(): print(f'>> [{vp_name}] {label}: {url}') try: page.goto(url, wait_until='networkidle', timeout=20000) time.sleep(2) path = os.path.join(OUT, f'{vp_name}_{label}.png') page.screenshot(path=path, full_page=True) print(f' -> {path}') except Exception as e: print(f' ERR: {e}') ctx.close() browser.close()