playwright-python 截图、录制视频、录制接口(二)
playwright-python截图、录制视频、录制接口截图fullPage为True时,截取全屏,默认为Falseclip截取特定部分的图片,{"x": float, "y": float, "width": float, "height": float}type指定图片类型,支持['jpeg', 'png'],默认为pngquality图片质量,值为0-100,不适用于png图像timeou
playwright-python 截图、录制视频、录制接口
截图
fullPage为True时,截取全屏,默认为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()
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)