playwright录制功能使用绕过登录操作
1、首先安装playwright
pip install playwright
2、 安装支持的浏览器
playwright install # 安装支持的浏览器:cr, chromium, ff, firefox, wk 和 webkit
3、接着在自己的项目下运行录制命令:
playwright codegen
4、接着会出现一个浏览器界面和Playwright inspector界面
5、接着输入要录制的 网站:例如:https://www.baidu.com/,然后回车
6、接着输入自己要查询的东西,比如ai,然后点击百度一下,这时就可以看到Playwright inspector录制的界面代码:
7、停止录制脚本,点击小红点,
8、然后在vscode ,新建一个文件test2025015baidu.py,将代码复制里面
import re
from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.locator("body").click()
page.goto("https://www.baidu.com/")
page.locator("#kw").click()
page.locator("#kw").fill("ai")
page.locator("#kw").press("Enter")
page.get_by_role("button", name="百度一下").click()
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
9、然后关掉浏览器
10、运行代码,鼠标右击
11、就可以看到自动运行之前的操作了