“ https://playwright.dev/python/docs/intro
pip install playwright# 安装浏览器playwright install# 安装指定浏览器playwright install webkitplaywright codegen --target python -o baidu_test.py -b chromium https://www.baidu.com/from playwright.sync_api import sync_playwrightwith sync_playwright() as p: browser = p.chromium.launch() page = browser.new_page() page.goto("https://playwright.dev")print(page.title()) browser.close()import asynciofrom playwright.async_api import async_playwrightasyncdefmain():asyncwith async_playwright() as p: browser = await p.chromium.launch() page = await browser.new_page()await page.goto("https://playwright.dev")print(await page.title())await browser.close()asyncio.run(main())from playwright.sync_api import sync_playwrightwith sync_playwright() as p:# 声明浏览器对象 browser = p.chromium.launch()# 声明页面对象 page = browser.new_page()# url:跳转页面url# timeout:超时时间,单位毫秒# wait_until: 操作成功判断条件,["commit", "domcontentloaded", "load", "networkidle", None],默认为load page.goto(url="https://www.baidu.com",timeout=60000,wait_until="load") browser.close()page.locator("css=button")# 直接填充page.locator("#username").fill("输入内容")# 逐字输入page.locator('#area').press_sequentially("输入内容")# 左键点击,force为强制点击page.locator("#submit").click(force=True)# 右击page.locator("#submit").click(button="right")# 双击page.locator("#submit").dbclick()# shift+左键点击page.locator("#submit").click(modifiers=["Shift"])# 鼠标悬浮page.locator("#submit").hover()# 点击所选中元素的指定位置page.locator("#submit").click(position={ "x": 0, "y": 0})# 点击触发选中或者取消选中page.locator("#sex").check()# 校验是否被选中expect(page.locator('#sex')).to_be_checked()# 单选(匹配值/标签)page.locator("#sex").select_option('男')# 单选(仅匹配标签)page.locator("#sex").select_option(label='男')# 多选page.locator("#sex").select_option(['red', 'green', 'blue'])# 快捷键列表Backquote, Minus, Equal, Backslash, Backspace, Tab, Delete, Escape,ArrowDown, End, Enter, Home, Insert, PageDown, PageUp, ArrowRight,ArrowUp, F1 - F12, Digit0 - Digit9, KeyA - KeyZ, etc.# 回车page.locator("#submit").press("Enter")# 单文件上传page.locator("#file").set_input_files('myfile.pdf')# 多文件上传page.locator("#file").set_input_files(['file1.txt', 'file2.txt'])# 清空上传的文件page.locator("#file").set_input_files([])# 方式一page.locator("#a").drag_to(page.locator("#b"))# 方式二page.locator("#a").hover()page.mouse.down()page.locator("#b").hover()page.mouse.up()断言列表
“ 当前目录下创建一个
.auth目录用于存储
# 保存当前状态storage = context.storage_state(path="state.json")# 使用之前的状态context = browser.new_context(storage_state="state.json")“ 必须监听
dialog事件
# 点击确认page.on("dialog", lambda dialog: dialog.accept())# 点击关闭page.on("dialog", lambda dialog: dialog.dismiss())# 自定义处理逻辑defhandle_dialog(dialog): # 具体逻辑 dialog.dismiss()page.on("dialog", lambda: handle_dialog)“
playwright.devices指定设备,设备列表
from playwright.sync_api import sync_playwright, Playwright defrun(playwright: Playwright): iphone_13 = playwright.devices['iPhone 13'] browser = playwright.webkit.launch(headless=False) context = browser.new_context( **iphone_13, )with sync_playwright() as playwright: run(playwright)# 直接执行page.evaluate('object => object.foo', { 'foo': 'bar' })button = page.evaluate_handle('window.button') page.evaluate('button => button.textContent', button)# 监听请求page.on("request", lambda request: print(">>", request.method, request.url))# 监听响应 page.on("response", lambda response: print("<<", response.status, response.url))page.screenshot(path="screenshot.png", full_page=True)page.locator(".header").screenshot(path="screenshot.png")