Download
Download 物件由頁面透過 page.on("download") 事件發送。
當瀏覽器內容關閉時,所有屬於該瀏覽器內容的下載檔案都會被刪除。
下載事件會在下載開始時發出。下載路徑會在下載完成後提供。
- 同步
- 非同步
# 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)
方法
cancel
新增於:v1.13取消下載。如果下載已完成或已取消,則不會失敗。成功取消後,download.failure()
將解析為 'canceled'
。
用法
download.cancel()
回傳
delete
v1.9 之前新增刪除已下載的檔案。如有必要,將等待下載完成。
用法
download.delete()
回傳
failure
v1.9 之前新增如果有的話,回傳下載錯誤。如有必要,將等待下載完成。
用法
download.failure()
回傳
path
v1.9 之前新增針對成功的下載,回傳下載檔案的路徑,或者針對失敗/取消的下載拋出例外。如有必要,此方法將等待下載完成。當遠端連線時,此方法會拋出例外。
請注意,下載的檔案名稱是隨機 GUID,使用 download.suggested_filename 以取得建議的檔案名稱。
用法
download.path()
回傳
save_as
v1.9 之前新增將下載複製到使用者指定的路徑。在下載仍在進行中時呼叫此方法是安全的。如有必要,將等待下載完成。
用法
- 同步
- 非同步
download.save_as("/path/to/save/at/" + download.suggested_filename)
await download.save_as("/path/to/save/at/" + download.suggested_filename)
引數
-
path
Union[str, pathlib.Path]#應複製下載的路徑。
回傳
屬性
page
新增於:v1.12取得下載所屬的頁面。
用法
download.page
回傳
suggested_filename
v1.9 之前新增回傳此下載的建議檔案名稱。它通常由瀏覽器根據 Content-Disposition
回應標頭或 download
屬性計算而得。請參閱關於 whatwg 的規範。不同的瀏覽器可以使用不同的邏輯來計算它。
用法
download.suggested_filename
回傳
url
v1.9 之前新增回傳已下載的 URL。
用法
download.url
回傳