2021-01-16 23:37:13 +01:00
---
id: downloads
title: "Downloads"
---
2022-09-07 12:41:12 +02:00
For every attachment downloaded by the page, [`event: Page.download`] event is emitted. All these attachments are 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 23:37:13 +01:00
You can specify where to persist downloaded files using the [`option: downloadsPath`] option in [`method: BrowserType.launch`].
:::note
2021-07-19 23:56:28 +02:00
Downloaded files are deleted when the browser context that produced them is closed.
2021-01-16 23:37:13 +01:00
:::
Here is the simplest way to handle the file download:
```js
2022-11-30 21:36:35 +01:00
// Start waiting for download before clicking. Note no await.
const downloadPromise = page.waitForEvent('download');
await page.getByText('Download file').click();
const download = await downloadPromise;
2021-01-16 23:37:13 +01:00
// Wait for the download process to complete
2022-07-18 22:37:21 +02:00
console.log(await download.path());
// Save downloaded file somewhere
await download.saveAs('/path/to/save/download/at.txt');
2021-01-16 23:37:13 +01:00
```
2021-08-11 06:23:57 +02:00
```java
// Wait for the download to start
Download download = page.waitForDownload(() -> {
// Perform the action that initiates download
2022-11-30 21:36:35 +01:00
page.getByText("Download file").click();
2021-08-11 06:23:57 +02:00
});
// Wait for the download process to complete
Path path = download.path();
2022-07-18 22:37:21 +02:00
System.out.println(download.path());
// Save downloaded file somewhere
download.saveAs(Paths.get("/path/to/save/download/at.txt"));
2021-08-11 06:23:57 +02:00
```
2021-01-16 23:37:13 +01:00
```python async
# Start waiting for the download
async with page.expect_download() as download_info:
# Perform the action that initiates download
2022-11-30 21:36:35 +01:00
await page.get_by_text("Download file").click()
2021-01-16 23:37:13 +01:00
download = await download_info.value
# Wait for the download process to complete
2022-07-18 22:37:21 +02:00
print(await download.path())
# Save downloaded file somewhere
2023-02-06 14:26:48 +01:00
await download.save_as("/path/to/save/download/at.txt")
2021-01-16 23:37:13 +01:00
```
```python sync
# Start waiting for the download
with page.expect_download() as download_info:
# Perform the action that initiates download
2022-11-30 21:36:35 +01:00
page.get_by_text("Download file").click()
# Wait for the download to start
2021-01-16 23:37:13 +01:00
download = download_info.value
# Wait for the download process to complete
2022-07-18 22:37:21 +02:00
print(download.path())
# Save downloaded file somewhere
download.save_as("/path/to/save/download/at.txt")
2021-01-16 23:37:13 +01:00
```
2021-05-20 04:53:12 +02:00
```csharp
2022-11-30 21:36:35 +01:00
// Start the task of waiting for the download before clicking
2021-05-20 04:53:12 +02:00
var waitForDownloadTask = page.WaitForDownloadAsync();
2022-11-30 21:36:35 +01:00
await page.GetByText("Download file").ClickAsync();
2021-05-20 04:53:12 +02:00
var download = await waitForDownloadTask;
2022-11-30 21:36:35 +01:00
// Wait for the download process to complete
2022-07-18 22:37:21 +02:00
Console.WriteLine(await download.PathAsync());
// Save downloaded file somewhere
await download.SaveAsAsync("/path/to/save/download/at.txt");
2021-05-20 04:53:12 +02:00
```
2021-01-16 23:37:13 +01: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 18:18:44 +01:00
```java
page.onDownload(download -> System.out.println(download.path()));
```
2021-01-16 23:37:13 +01: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);
```
2022-09-07 12:41:12 +02:00
Note that handling the event forks the control flow and makes the 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-16 23:37:13 +01:00
2022-09-07 12:41:12 +02:00
:::note
For uploading files, see the [uploading files ](./input.md#upload-files ) section.
:::