下載
簡介
每個頁面下載的附件都會觸發 page.on("download") 事件。所有這些附件都會下載到一個臨時文件夾中。您可以使用事件中的 Download 物件獲取下載的 URL、文件名和有效負載流。
您可以使用 downloads_path
選項來指定下載的檔案儲存位置 browser_type.launch()。
note
下載的檔案會在產生它們的瀏覽器上下文關閉時被刪除。
以下是處理檔案下載的最簡單方法:
- 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)