Download
Download 对象通过 page.on("download") 事件按页分派。
¥Download objects are dispatched by page via the page.on("download") event.
当浏览器上下文关闭时,属于浏览器上下文的所有下载文件都将被删除。
¥All the downloaded files belonging to the browser context are deleted when the browser context is closed.
下载开始后就会触发下载事件。下载完成后,下载路径变为可用。
¥Download event is emitted once the download starts. Download path becomes available once download completes.
- Sync
- Async
# Start waiting for the download
with page.expect_download() as download_info:
# Perform the action that initiates download
page.get_by_text("Download file").click()
download = download_info.value
# Wait for the download process to complete and save the downloaded file somewhere
download.save_as("/path/to/save/at/" + download.suggested_filename)
# Start waiting for the download
async with page.expect_download() as download_info:
# Perform the action that initiates download
await page.get_by_text("Download file").click()
download = await download_info.value
# Wait for the download process to complete and save the downloaded file somewhere
await download.save_as("/path/to/save/at/" + download.suggested_filename)
方法
¥Methods
cancel
Added in: v1.13取消下载。如果下载已经完成或取消,则不会失败。成功取消后,download.failure()
将解析为 'canceled'
。
¥Cancels a download. Will not fail if the download is already finished or canceled. Upon successful cancellations, download.failure()
would resolve to 'canceled'
.
用法
¥Usage
download.cancel()
返回
¥Returns
delete
Added before v1.9删除下载的文件。如有必要,将等待下载完成。
¥Deletes the downloaded file. Will wait for the download to finish if necessary.
用法
¥Usage
download.delete()
返回
¥Returns
failure
Added before v1.9如果有则返回下载错误。如有必要,将等待下载完成。
¥Returns download error if any. Will wait for the download to finish if necessary.
用法
¥Usage
download.failure()
返回
¥Returns
path
Added before v1.9成功下载时返回已下载文件的路径,下载失败/取消时抛出异常。如有必要,该方法将等待下载完成。远程连接时该方法会抛出异常。
¥Returns path to the downloaded file for a successful download, or throws for a failed/canceled download. The method will wait for the download to finish if necessary. The method throws when connected remotely.
请注意,下载的文件名是随机 GUID,使用 download.suggested_filename 获取建议的文件名。
¥Note that the download's file name is a random GUID, use download.suggested_filename to get suggested file name.
用法
¥Usage
download.path()
返回
¥Returns
save_as
Added before v1.9将下载内容复制到用户指定的路径。在下载仍在进行时调用此方法是安全的。如有必要,将等待下载完成。
¥Copy the download to a user-specified path. It is safe to call this method while the download is still in progress. Will wait for the download to finish if necessary.
用法
¥Usage
- Sync
- Async
download.save_as("/path/to/save/at/" + download.suggested_filename)
await download.save_as("/path/to/save/at/" + download.suggested_filename)
参数
¥Arguments
path
Union[str, pathlib.Path]#
应复制下载的路径。
¥Path where the download should be copied.
返回
¥Returns
属性
¥Properties
page
Added in: v1.12获取下载所属的页面。
¥Get the page that the download belongs to.
用法
¥Usage
download.page
返回
¥Returns
suggested_filename
Added before v1.9返回此下载的建议文件名。它通常由浏览器根据 Content-Disposition
响应标头或 download
属性计算得出。请参阅 whatwg 上的规范。不同的浏览器可以使用不同的逻辑来计算它。
¥Returns suggested filename for this download. It is typically computed by the browser from the Content-Disposition
response header or the download
attribute. See the spec on whatwg. Different browsers can use different logic for computing it.
用法
¥Usage
download.suggested_filename
返回
¥Returns
url
Added before v1.9返回下载的 url。
¥Returns downloaded url.
用法
¥Usage
download.url
返回
¥Returns