playwright中文文档
Playwright 为现代 web 应用提供了跨浏览器、快速且可靠的端到端的测试能力。 | Playwright 中文文档 | Playwright 中文网 (bootcss.com)
playwright安装pip install playwright
playwright-浏览器安装playwright的nohead模式需要在本地安装浏览器的内核
playwright install
交互方式playwright直接操作浏览器,不在需要对应的driver层
playwright-demo 同步模式with sync_playwright() as p: #启动浏览器,默认是True无头模式 browser = p.chromium.launch(headless=False) #打开一个标签页 page = browser.new_page() #访问目标网址 page.goto("https://www.baidu.com") print(page.title) #关闭浏览器 broswer.close()异步模式-python3.7特性
import asyncio from playwright.async_api import async_playwright with async_playwright() as p: browser = await p.chromium.launch(headless = True) page = await browser.new_page() await page.goto("https://www.baidu.com) print(await page.title) await browser.close()playwright-脚本录制生成
playwright codegen dest_url
playwright codegen playwright.dev #打开一个空白页面
playwright-pytest运行pip install pytest-playwright
playwright-登录token 存储tokenplaywright codegen --save-storage=auth.json
使用tokenplaywright codegen --load-storage=auth.json destination_url
playwright-元素拾取器 pick locator playwright-console定位 playwright-断点page.pause()
playwright-页面最大化browser = p.chromium.launch(headless=False, args=[--start-maximiszed]) context = browser.new_context(no_viewport=True) page = context.new_page()
或者
browser = p.chromium.launch(headless=False, args=[--start-maximiszed]) page = browser.new_page(no_viewport=True)playwright-浏览器选择
browser = p.chromium.launch(channel="msedge")playwright-使用本地缓存
context = p.chromium.launch_persistent_context(user_data_dir=Path})
playwright-录制视频
browser = p.chromium.launch(headless=False, slow_mo=500) context = browser.new_content(record_video_dir="videos/") context.close()playwright-trace追踪
context = browser.new_context() context.tracing.start(screenshots=True, snapshots=True, sources=True) page = context.new_page() context.tracing.stop(path="trace.zip")
trace数据会保存在trace.zip下
trace数据读取方法playwright show-trace trace.zip
playwright-等待页面加载完成with page.expect_navigation(url="**/GPT-4"): page.get_by_role("button", name="xxx").click()playwright-使用cookie
context = browser.new_context(storage_state="auth.json")标签:playwright,webui,context,自动化,new,chromium,page,browser From: https://www.cnblogs.com/watch2learn/p/18084815