playwright使用浏览器插件
时间: 2024-01-04 22:03:59 浏览: 148
playwright-pytest:Pytest插件可使用Playwright编写端到端浏览器测试
以下是使用Playwright连接已有浏览器操作的示例代码:
```python
from playwright.sync_api import sync_playwright
import time
# 启动Playwright
playwright = sync_playwright().start()
# 连接已启动的浏览器
browser = playwright.chromium.connect_over_cdp("http://localhost:9222")
# 创建新页面
page = browser.new_page()
# 在页面上执行操作
page.goto("https://www.baidu.com/")
print(page.title())
# 关闭浏览器
browser.close()
# 停止Playwright
playwright.stop()
```
这段代码使用Playwright的`connect_over_cdp`方法连接到已启动的浏览器,并在页面上执行操作。你需要将`http://localhost:9222`替换为实际的浏览器调试地址。
阅读全文