playwright下载
时间: 2023-10-11 08:14:27 浏览: 158
playwright中的下载操作相对于selenium来说更简便一些。在playwright中,可以使用`with page.expect_download() as download_info`来等待下载完成,然后通过`download_info.value`获取下载的相关元素,最后通过`download.path()`获取下载的路径。以下是示例代码:
```
import time
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context(accept_downloads=True)
page = context.new_page()
# 执行下载操作
with page.expect_download() as download_info:
page.click("button#download-button")
download = download_info.value
path = download.path()
# 打印下载路径
print(path)
```
以上代码中,`page.click("button#download-button")`是执行启动下载的操作,`download_info.value`获取到了下载的相关元素,然后赋值给`download`,最后通过`download.path()`获取下载的路径。
阅读全文