playwright 点击后页面跳转 python
时间: 2023-06-18 11:02:38 浏览: 488
你可以使用 Playwright 在 Python 中模拟用户点击操作并跳转到新页面。首先,你需要使用 Playwright 的 Python 版本,并导入所需的模块:
```python
from playwright.sync_api import Playwright, sync_playwright
```
然后,你可以使用 `with` 语句打开一个浏览器:
```python
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
```
接下来,你可以使用 `goto` 方法跳转到要点击的页面,并使用 `click` 方法模拟点击操作:
```python
page.goto('https://example.com')
page.click('a#link')
```
最后,你可以使用 `wait_for_url` 方法等待页面跳转完成:
```python
page.wait_for_url('https://example.com/new-page')
```
完整的示例代码如下:
```python
from playwright.sync_api import Playwright, sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto('https://example.com')
page.click('a#link')
page.wait_for_url('https://example.com/new-page')
browser.close()
```
这个示例使用 Chromium 浏览器,你可以根据需要更改为其他浏览器。
阅读全文