Playwright Example >> Download File

2022-03-25 Playwright

Table of Contents

In this article, we will use Playwright (Python version) to download a file automatically and save it to specified path.

Playwright Download file

Download a file

We will use demo page for downloading file of demoqa.com to test our code.
https://demoqa.com/upload-download

On the page, there is a button named Download.

Playwright Download file

Firstly, we should open the web page.

page.goto("https://demoqa.com/upload-download")

We should wait for the download result because it will take some time for downloading a file after clicking download button.
We can use expect_download() method of page object to wait for download finished. When download finished, the information of download will be returned.
You can download a file using Playwright as below.

with page.expect_download() as download_info:
    page.locator("a:has-text(\"Download\")").click()

The download_info will accept download result.

Save the downloaded file

We can get the download result from value property of download_info.

download = download_info.value

And we can get the original file name using suggested_filename property of download. It is typically computed by the browser from the Content-Disposition response header or the download attribute.

file_name = download.suggested_filename

Next, let’s decide where to save this file. Here we will save the file to data folder and use relative path.

destination_folder_path = "./data/"

Finally, let’s save the download result to the specified destination path.
Here we use the original file name, of course you can specify your own file name.

download.save_as(os.path.join(destination_folder_path, file_name))

Below is the complete code.

from playwright.sync_api import Playwright, sync_playwright, expect
import os

def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()

    # Open new page
    page = context.new_page()

    # Go to https://demoqa.com/upload-download
    page.goto("https://demoqa.com/upload-download")

    # Click a:has-text("Download")
    with page.expect_download() as download_info:
        page.locator("a:has-text(\"Download\")").click()
    download = download_info.value
    file_name = download.suggested_filename
    destination_folder_path = "./data/"
    download.save_as(os.path.join(destination_folder_path, file_name))

    # ---------------------
    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)

When execution finished, you should see the downloaded image file in your data folder.

Playwright Download file

Conclusion

We can wait for download result returning by using expect_download() method after clicking the download button. And use download info to get the original file name and use save_as() method to save the downloaded file to specified path as specified file name.

If you want to learn more about Playwright usage, you can read the article below.

Playwright » Basic Usage
https://thats-it-code.com/playwright/playwright__basic-usage/

You can read the articles below to create a programming environment in your PC.

Let’s Create a Programming Environment
https://thats-it-code.com/programming/lets-create-a-programming-environment/

How to Create Python Virtual Environment
https://thats-it-code.com/programming/how-to-create-python-virtual-environment/

Subscribe and be the FIRST reader of our latest articles

* indicates required

Contact us