Rayrun
← Back to Discord Forum

How to download PDF using playwright?

vipinphogatposted in #help-playwright
Open in Discord
vipinphogat

I have a page on which there is a download link. In my test i want to click on the link and download the file. I am running the test on chrome and whenever i click on the link using playwright instead of download the pdf document is displayed in chromium’s internal pdf viewer. I looked at multiple similar issues mentioned on playwright git repo but none of them seems to be working. Has anyone implemented the pdf download step in any of the test cases?

This thread is trying to answer question "How can I download a PDF using Playwright when the PDF opens in Chromium's internal viewer instead of downloading?"

14 replies

export default defineConfig({ use: { acceptDownloads: false, }, });

Have you configured the browser to acceptDownloads?

The default is true so shouldn't be the issue actually

do you have a listener set up for the download?

vipinphogat

Right… i also tried with page.waitForEvent(‘download’) but still the pdf is opening instead of downloading. It seems the listener works only for non pdf docs

Could you use page.pdf() to generate a pdf of the page once it opens in a new tab?

I know it isn't the greatest solution

vipinphogat

This will generate the pdf of current page. I want to download the pdf after i click on the link to which it is attached

Right but if it generates "chromium’s internal pdf viewer." then surely this would generate the pdf you want.

Not suggesting this as the final solution but a potential work around

You could do something similar to this -

Get the href from the "download pdf" button

href = page.locator("a.download-pdf").get_attribute('href')
absolute_url = f"https://arxiv.org{href}"

# Download the file using requests
file = requests.get(absolute_url)
with open('output.pdf', 'wb') as f:
    f.write(file.content)
vipinphogat

I will try this and get back with results… thanks for quick reply

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.