Skip to content
Advertisement

How to download PDF files with Playwright? (Python)

I’m trying to automate the download of a PDF file using Playwright, I’ve the code working with Selenium, but some features in Playwright got my attention. The real problem the documentation isn’t helpful. When I click on download I get this:
enter image description here

And I cant change the directory of the download, it also delete the “file” when the browser/context are closed. Using Playwright I can achieve a nice download automation?

Code:

def run(playwright):
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context(accept_downloads=True)

    # Open new page
    page = context.new_page()

    # Go to http://xcal1.vodafone.co.uk/
    page.goto("http://xcal1.vodafone.co.uk/")

    # Click text=Extra Small File 5 MB A high quality 5 minute MP3 music file 30secs @ 2 Mbps 10s >> img
    with page.expect_download() as download_info:
        page.click("text=Extra Small File 5 MB A high quality 5 minute MP3 music file 30secs @ 2 Mbps 10s >> img")
    download = download_info.value
    path = download.path()
    download.save_as(path)
    print(path)

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

with sync_playwright() as playwright:
    run(playwright)

Advertisement

Answer

The download.path() in playwright is just a random GUID (globally unique identifier). It’s designed to validate the download works – not to keep the file.

Playwright is a testing tool and imagine running tests across every major browser on every code change – any downloads would quickly take up a lot of space and it would hack people off if you need to manually clear them out.

Good news is you are very close – If you want to keep the file you just need to give the file a name in the save_as.

instead of this:

   download.save_as(path)

use this:

   download.save_as(download.suggested_filename)

That saves the file in the same location as the script.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement