如何通过playwright 控制它 python
时间: 2023-06-09 08:05:02 浏览: 90
python-playwright修改GET/POST请求参数,抓包修改数据
您可以使用 Python Playwright 库来控制 Playwright。您需要安装 Playwright 和 Python Playwright,并编写 Python 代码来控制它。以下是一些示例代码:
1. 安装 Playwright 和 Python Playwright:
```bash
npm i playwright
pip install playwright
```
2. 编写 Python 代码:
```python
from playwright.sync_api import Playwright, async_playwright
async def run(playwright: Playwright) -> None:
# 启动 Chromium 浏览器
browser = await playwright.chromium.launch()
# 创建一个新的页面
page = await browser.new_page()
# 跳转到百度
await page.goto('https://www.baidu.com/')
# 获取搜索框对象
search_input = await page.query_selector('#kw')
# 在搜索框中输入关键字
await search_input.type('Python')
# 获取搜索按钮对象
search_button = await page.query_selector('#su')
# 点击搜索按钮
await search_button.click()
# 等待搜索结果加载完毕
await page.wait_for_selector('#content_left')
# 关闭浏览器
await browser.close()
with async_playwright() as playwright:
# 启动 run 函数
playwright.run(run)
```
这段代码将启动 Chromium 浏览器,然后打开百度,输入关键字并点击搜索按钮,等待结果加载完毕后关闭浏览器。您可以根据需要修改此代码来控制 Playwright。
阅读全文