← Back to Discord Forum

How to write PDF download options using playwright?

_henryxie999posted in #help-playwright
Open in Discord

Hi all, I am going to download PDF file automatically when I click the button. I implemented this using Selenium before but now I want to do this using playwright. Now when I click the button, internal PDF viewer opens it with a new browser. So I think if I make the internal PDF viewer disabled, it is possible to download PDF file automatically, am I right?

This is my selenium code.

chromeOptions = webdriver.ChromeOptions() prefs = {"plugins.plugins_disabled" : ["Chrome PDF Viewer"]} # Here should be a list chromeOptions.add_experimental_option("prefs",prefs) chromedriver = "path/to/chromedriver.exe" driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)

How to do with playwright? I am working with this for 3 days but not resolved. I am looking forward to your kind replies.

Kind regards!

This thread is trying to answer question "How can I download PDF files automatically using Playwright?"

4 replies
await page.locator('').click();
page.on('request', (request: Request) => {
    if (request.url().endsWith('.pdf')) {
      request.continue({ url: 'file:///path/to/save/file.pdf' });
    } else {

You need to import Request from playwright


This feature request contains some useful information: https://github.com/microsoft/playwright/issues/7822

It also contains a workaround for Chromium (sorry it's JavaScript, somebody else will have to translate to Python):

await page.route('**/empty.pdf', async route => {
    const response = await page.context().request.get(route.request());
    await route.fulfill({
      headers: {
        'Content-Disposition': 'attachment',

So it:

  • Intercepts the requests for any PDFs with file name empty.pdf (you might want to change that)
  • Then does the request to get the original response
  • Then fulfils the request (returns the response) with a modified Content-Disposition to tell the browser to handle it as an attachment.

Hi @inf , thanks for your help


I have some questions

Related Discord Threads

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.