用playwright截图

91 阅读1分钟

截图可以保留测量时的证据,用来跟我们的客户说明方案

from playwright.sync_api import sync_playwright

def screenshot_with_options(url, output_path):
    with sync_playwright() as p:
        # 启动浏览器
        browser = p.chromium.launch(headless=False)
        context = browser.new_context()
        page = context.new_page()

        # 导航到目标页面
        page.goto(url)

        # 对整个页面进行截图,并指定截图区域
        page.screenshot(path=output_path, clip={"x": 0, "y": 0, "width": 800, "height": 600})

        # 关闭浏览器
        browser.close()

# 示例调用
url = "https://example.com"
output_path = "screenshot_with_clip.png"
screenshot_with_options(url, output_path)