python中用playwright的page.screenshot,设置截屏路径
【代码】python中用playwright的page.screenshot,设置截屏路径。
·
from playwright.sync_api import sync_playwright
def run(playwright):
browser = playwright.chromium.launch(headless=False) # 启动浏览器,headless=False 以便看到浏览器操作
context = browser.new_context()
page = context.new_page()
# 导航到网页
page.goto('https://www.example.com')
# 设置截屏路径
screenshot_path = './screenshots/example.png' # 确保目录存在,或者先创建目录
# 截取屏幕截图并保存到指定路径
page.screenshot(path=screenshot_path)
# 打印截屏路径以确认保存位置
print(f"Screenshot saved at: {screenshot_path}")
# 关闭浏览器
browser.close()
with sync_playwright() as playwright:
run(playwright)
更多推荐
已为社区贡献1条内容
所有评论(0)