截圖
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()