     [Answers](https://scrapfly.io/blog)   /  [playwright](https://scrapfly.io/blog/tag/playwright)   /  [How to download a file with Playwright and Python?](https://scrapfly.io/blog/answers/how-to-download-file-with-playwright)   # How to download a file with Playwright and Python?

 by [Bernardas Alisauskas](https://scrapfly.io/blog/author/bernardas) Dec 05, 2022 1 min read [\#playwright](https://scrapfly.io/blog/tag/playwright) 

 [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-download-file-with-playwright "Share on LinkedIn")    

 

 

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:

python```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)
```





 

   Table of Contents















 

   Join the Newsletter  Get monthly web scraping insights 

 

  



Scale Your Web Scraping

Anti-bot bypass, browser rendering, and rotating proxies, all in one API. Start with 1,000 free credits.

  No credit card required  1,000 free API credits  Anti-bot bypass included 

 [Start Free](https://scrapfly.io/register) [View Docs](https://scrapfly.io/docs/onboarding) 

 Not ready? Get our newsletter instead. 

 

## Explore this Article with AI

 [ ChatGPT ](https://chat.openai.com/?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-download-file-with-playwright) [ Gemini ](https://www.google.com/search?udm=50&aep=11&q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-download-file-with-playwright) [ Grok ](https://x.com/i/grok?text=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-download-file-with-playwright) [ Perplexity ](https://www.perplexity.ai/search/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-download-file-with-playwright) [ Claude ](https://claude.ai/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-download-file-with-playwright) 



 ## Related Articles

 [     

### How to Use cURL to Download Files

Curlhttps://curl.se/, short for "Client URL," is a versatile command-line tool used for transferring data with URLs. It'...

 

 ](https://scrapfly.io/blog/posts/how-to-curl-download-file) [     

### What is HTTP 405 Error? (Method Not Allowed)

HTTP error codes can be confusing, especially when they disrupt your web scraping or automation tasks. One such error is...

 

 ](https://scrapfly.io/blog/posts/what-is-http-405-error) [  

 http 

### What is HTTP 409 Error? (Conflict)

HTTP status code 409 generally means a conflict or mismatch with the server state. Learn why it happens and how to avoid...

 

 ](https://scrapfly.io/blog/posts/what-is-http-409-status-code-conflict) 

  ## Related Questions

- [ Q How to download a file with Puppeteer? ](https://scrapfly.io/blog/answers/how-to-download-file-with-puppeteer)
- [ Q How to click on cookie popups and modal alerts in Playwright? ](https://scrapfly.io/blog/answers/how-to-click-on-modal-alerts-like-cookie-pop-up-in-playwright)
- [ Q How to load local files in Puppeteer? ](https://scrapfly.io/blog/answers/how-to-load-local-files-in-puppeteer)
- [ Q How to click on cookie popups and modal alerts in Selenium? ](https://scrapfly.io/blog/answers/how-to-click-on-modal-alerts-like-cookie-pop-up-in-selenium)
 
  



   



 Run headless browsers at scale, **1,000 free credits** [Start Free](https://scrapfly.io/register)