How to download a file with Playwright and Python
Playwright can reach a file in more than one way.
You can point a Locator at the page's download control and click it, or you can read the link target from an anchor with get_attribute and retrieve the file with requests. The second route is often safer for PDFs and other documents, because a click may open the file in the browser instead of starting a download.
Approach A: click the download control
Find the element that starts the download — a button, link, or icon — with any Locator strategy you already use, then click it. This works when the page genuinely triggers a download response. It is the shortest path when the control behaves as expected.
Approach B: read the anchor href and fetch with requests
Instead of clicking, locate the anchor that holds the file URL and read its href attribute with get_attribute. Build the absolute URL if the href is relative, then fetch it with requests and write the response body to disk. This avoids the case where a PDF or similar file opens natively in the browser rather than downloading on click.
Which approach should you use?
Use the checklist below to pick a route before writing code.
What the sample workflow touches
The sample below uses a fixed arXiv paper page and a download-pdf anchor. The table maps each step to the value it produces.
| Situation | Preferred approach | Why |
|---|---|---|
| The control reliably starts a download | Click the Locator | Fewest steps; Playwright handles the download event |
| The file is a PDF that may open in the browser | Read href and fetch with requests | Avoids the native viewer intercepting the click |
| The href is relative | Read href, then build absolute URL | requests needs a full URL |
| You need the raw bytes on disk | Either, then write response content | Both routes end with file bytes |
Guidance derived from the two described approaches; not a benchmark.
| Step | Value / call | Purpose |
|---|---|---|
| Launch browser | p.chromium.launch(headless=False) | Start a visible Chromium instance |
| Open page | page.goto('https://arxiv.org/abs/2301.05226') | Navigate to the paper page |
| Locate download anchor | page.locator("a.download-pdf") | Target the PDF link |
| Read href | get_attribute('href') | Extract the relative file path |
| Build absolute URL | f"https://arxiv.org{href}" | Make the URL fetchable |
| Fetch bytes | requests.get(absolute_url) | Download the file content |
| Write to disk | open('output.pdf', 'wb') | Save the response body as a PDF |
Values reflect the sample code; the arXiv URL and selector are illustrative, not universal.
Frequently asked questions
Why fetch the href with requests instead of clicking?
Because PDFs and other downloadable files sometimes open in the browser's native viewer instead of starting a download when the control is clicked. Reading the href and fetching it with requests sidesteps that behavior.
What if the href is relative?
Combine it with the site's origin to form an absolute URL before passing it to requests. The sample prefixes the arXiv origin to the href value.
Does the browser need to be visible?
The sample launches Chromium with headless=False, but the approach itself does not require a visible browser. Visibility is a debugging convenience, not a requirement of either download route.