playwright 获取当前页面地址
时间: 2023-08-12 16:04:49 浏览: 790
Playwright为页面中的某个请求单独设置代理
您可以使用Playwright的`page.url()`方法来获取当前页面地址。以下是一个示例:
```python
import asyncio
from playwright.sync_api import sync_playwright
async def get_page_url():
async with sync_playwright() as playwright:
browser = await playwright.chromium.launch()
page = await browser.new_page()
await page.goto('https://www.example.com')
url = page.url
print(url)
await browser.close()
asyncio.run(get_page_url())
```
在此示例中,我们使用了Playwright的`sync_playwright`模块,以同步方式启动Playwright。然后,我们创建了一个新的浏览器页面,并导航到了示例网站。最后,我们通过`page.url`属性获取当前页面的URL,并打印出来。请确保在使用之前已经安装了Playwright库。
请注意,Playwright还支持其他浏览器,如Firefox和WebKit。您可以根据需要选择不同的浏览器引擎来执行相同的操作。
阅读全文