2021-01-16 14:37:13 -08:00
|
|
|
---
|
|
|
|
id: downloads
|
|
|
|
title: "Downloads"
|
|
|
|
---
|
|
|
|
|
|
|
|
:::note
|
|
|
|
For uploading files, see the [uploading files](./input.md#upload-files) section.
|
|
|
|
:::
|
|
|
|
|
2021-12-06 09:25:24 -08:00
|
|
|
For every attachment downloaded by the page, [`event: Page.download`] event is emitted. All these attachments are going
|
|
|
|
to be downloaded into a temporary folder. You can obtain the download url, file system path and payload stream using
|
|
|
|
the [Download] object from the event.
|
2021-01-16 14:37:13 -08:00
|
|
|
|
|
|
|
You can specify where to persist downloaded files using the [`option: downloadsPath`] option in [`method: BrowserType.launch`].
|
|
|
|
|
|
|
|
:::note
|
2021-07-19 14:56:28 -07:00
|
|
|
Downloaded files are deleted when the browser context that produced them is closed.
|
2021-01-16 14:37:13 -08:00
|
|
|
:::
|
|
|
|
|
|
|
|
Here is the simplest way to handle the file download:
|
|
|
|
|
|
|
|
```js
|
|
|
|
const [ download ] = await Promise.all([
|
|
|
|
// Start waiting for the download
|
|
|
|
page.waitForEvent('download'),
|
|
|
|
// Perform the action that initiates download
|
|
|
|
page.click('button#delayed-download')
|
|
|
|
]);
|
|
|
|
// Wait for the download process to complete
|
|
|
|
const path = await download.path();
|
|
|
|
```
|
|
|
|
|
2021-08-10 21:23:57 -07:00
|
|
|
```java
|
|
|
|
// Wait for the download to start
|
|
|
|
Download download = page.waitForDownload(() -> {
|
|
|
|
// Perform the action that initiates download
|
|
|
|
page.click("button#delayed-download");
|
|
|
|
});
|
|
|
|
// Wait for the download process to complete
|
|
|
|
Path path = download.path();
|
|
|
|
```
|
|
|
|
|
2021-01-16 14:37:13 -08:00
|
|
|
```python async
|
|
|
|
# Start waiting for the download
|
|
|
|
async with page.expect_download() as download_info:
|
|
|
|
# Perform the action that initiates download
|
|
|
|
await page.click("button#delayed-download")
|
|
|
|
download = await download_info.value
|
|
|
|
# Wait for the download process to complete
|
|
|
|
path = await download.path()
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
# Start waiting for the download
|
|
|
|
with page.expect_download() as download_info:
|
|
|
|
# Perform the action that initiates download
|
|
|
|
page.click("button#delayed-download")
|
|
|
|
download = download_info.value
|
|
|
|
# Wait for the download process to complete
|
|
|
|
path = download.path()
|
|
|
|
```
|
|
|
|
|
2021-05-20 04:53:12 +02:00
|
|
|
```csharp
|
|
|
|
// Start the task of waiting for the download
|
|
|
|
var waitForDownloadTask = page.WaitForDownloadAsync();
|
|
|
|
// Perform the action that initiates download
|
|
|
|
await page.ClickAsync("#downloadButton");
|
|
|
|
// Wait for the download process to complete
|
|
|
|
var download = await waitForDownloadTask;
|
2021-06-10 13:43:42 -07:00
|
|
|
var path = await download.PathAsync();
|
2021-05-20 04:53:12 +02:00
|
|
|
```
|
|
|
|
|
2021-01-16 14:37:13 -08:00
|
|
|
#### Variations
|
|
|
|
|
|
|
|
If you have no idea what initiates the download, you can still handle the event:
|
|
|
|
|
|
|
|
```js
|
|
|
|
page.on('download', download => download.path().then(console.log));
|
|
|
|
```
|
|
|
|
|
2021-03-01 09:18:44 -08:00
|
|
|
```java
|
|
|
|
page.onDownload(download -> System.out.println(download.path()));
|
|
|
|
```
|
|
|
|
|
2021-01-16 14:37:13 -08:00
|
|
|
```python async
|
|
|
|
async def handle_download(download):
|
|
|
|
print(await download.path())
|
|
|
|
page.on("download", handle_download)
|
|
|
|
```
|
|
|
|
|
|
|
|
```python sync
|
|
|
|
page.on("download", lambda download: print(download.path()))
|
|
|
|
```
|
|
|
|
|
2021-05-20 04:53:12 +02:00
|
|
|
```csharp
|
|
|
|
page.Download += (sender, download) => Console.WriteLine(download.Url);
|
|
|
|
```
|
|
|
|
|
2021-01-16 14:37:13 -08:00
|
|
|
Note that handling the event forks the control flow and makes script harder to follow. Your scenario might end while you
|
|
|
|
are downloading a file since your main control flow is not awaiting for this operation to resolve.
|
|
|
|
|
2021-01-17 21:09:40 -08:00
|
|
|
### API reference
|
2021-01-16 14:37:13 -08:00
|
|
|
- [Download]
|
|
|
|
- [`event: Page.download`]
|