playwright-python 截图、录制视频、录制接口

截图

fullPageTrue时,截取全屏,默认为False

clip截取特定部分的图片,{"x": float, "y": float, "width": float, "height": float}

type指定图片类型,支持['jpeg', 'png'],默认为png

quality图片质量,值为0-100,不适用于png图像

timeout超时时间,默认为30s

omitBackground隐藏默认的白色背景,允许透明截图。不适用于jpeg图像。默认False

from playwright import sync_playwright

with sync_playwright() as p:
    browser_type = p.chromium
    # browser = browser_type.launch(headless=False,slowMo=50,executablePath=r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
    browser = browser_type.launch(headless=False)
    context = browser.newContext()
    page = context.newPage()
    page.goto('https://www.baidu.com/')
    page.screenshot(path=f'example-{browser_type.name}.png',fullPage=True)
    context.close()
    browser.close()

录制视频

recordVideo参数设置视频地址和录制屏幕的大小,dir必须存在,否则不能录制;size不填默认录制屏幕大小;如果填写,则录取指定长宽的区域

from playwright import sync_playwright

with sync_playwright() as p:
    browser_type = p.chromium
    # browser = browser_type.launch(headless=False,slowMo=50,executablePath=r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
    browser = browser_type.launch(headless=False)
    context = browser.newContext(recordVideo={"dir": "./videos","size":{"width": 20, "height": 30}})
    page = context.newPage()
    page.goto('https://www.baidu.com/')
    context.close()
    browser.close()

录制接口

recordHar参数设置Har地址和是否忽略内容,path必须存在,否则不能录制;omitContent不填默认为False`,不忽略内容;如果为True,则忽略内容

from playwright import sync_playwright

with sync_playwright() as p:
    browser_type = p.chromium
    # browser = browser_type.launch(headless=False,slowMo=50,executablePath=r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")
    browser = browser_type.launch(headless=False)
    context = browser.newContext(recordHar={"path": "./test.har","omitContent":True})
    page = context.newPage()
    page.goto('https://www.baidu.com/')
    context.close()
    browser.close()
上一篇:Mac设置Root密码


下一篇:python工具——playwright