Skip to content

How to download a file with Playwright and Python

Playwright can reach a file in more than one way.

OUTPUT FORMATSHTMLJSONMarkdown

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.

Choosing between click and href fetch
SituationPreferred approachWhy
The control reliably starts a downloadClick the LocatorFewest steps; Playwright handles the download event
The file is a PDF that may open in the browserRead href and fetch with requestsAvoids the native viewer intercepting the click
The href is relativeRead href, then build absolute URLrequests needs a full URL
You need the raw bytes on diskEither, then write response contentBoth routes end with file bytes

Guidance derived from the two described approaches; not a benchmark.

Sample workflow field map
StepValue / callPurpose
Launch browserp.chromium.launch(headless=False)Start a visible Chromium instance
Open pagepage.goto('https://arxiv.org/abs/2301.05226')Navigate to the paper page
Locate download anchorpage.locator("a.download-pdf")Target the PDF link
Read hrefget_attribute('href')Extract the relative file path
Build absolute URLf"https://arxiv.org{href}"Make the URL fetchable
Fetch bytesrequests.get(absolute_url)Download the file content
Write to diskopen('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.