How to download a file with Playwright and Python?

To download files using Playwright we can either find the download button/link using the locator function and then click it or we can download it using HTTP client like httpx or requests in Python:

from pathlib import Path
from playwright.sync_api import sync_playwright
import httpx  # or import requests

def download_file_with_playwright():
    with sync_playwright() as pw:
        browser = pw.chromium.launch(headless=False)
        context = browser.new_context(viewport={"width": 1920, "height": 1080})

        page = context.new_page()
        page.goto('https://httpbin.dev/html')

        # we can either click the download button using locator:
        file = page.locator('a')
        file.click()

        # or we can download the file manually which is more flexible and faster
        url = file.get_attribute('href')
        response = httpx.get(url)
        Path('file.txt').write_bytes(response.content)
Question tagged: Playwright

Related Posts

How to Scrape Google Maps

We'll take a look at to find businesses through Google Maps search system and how to scrape their details using either Selenium, Playwright or ScrapFly's javascript rendering feature - all of that in Python.

How to Scrape Dynamic Websites Using Headless Web Browsers

Introduction to using web automation tools such as Puppeteer, Playwright, Selenium and ScrapFly to render dynamic websites for web scraping