title: How to catch a download with playwright?
body: <p>I'm trying to download a file from a website using <a href="https://github.com/microsoft/playwright" rel="nofollow noreferrer">Playwright</a>. The button that triggers the download does some js and then the download starts.</p>
  
  <p>Clicking the button using the <code>.click</code> function triggers a download but it shows an error: <em>Failed - Download error</em>. </p>
  
  <p><a href="https://i.stack.imgur.com/4DK1g.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4DK1g.png" alt="enter image description here"></a></p>
  
  <p>I've tried using the  devtools protocol <a href="https://vanilla.aslushnikov.com/?Page.setDownloadBehavior" rel="nofollow noreferrer"><code>Page.setDownloadBehavior</code></a>, but this doesn't seem to do anything.</p>
  
  <pre class="lang-js prettyprint-override"><code>    const playwright = require("playwright");
      const { /*chromium,*/ devices } = require("playwright");
      const iPhone = devices["iPad (gen 7) landscape"];
  
      (async () =&gt; {
          const my_chromium = playwright["chromium"];
          const browser = await my_chromium.launch({ headless: false });
          const context = await browser.newContext({
              viewport: iPhone.viewport,
              userAgent: iPhone.userAgent
          });
          const page = await context.newPage();
          const client = await browser.pageTarget(page).createCDPSession();
          console.log(client);
          await client.send("Page.setDownloadBehavior", {
              behavior: "allow",
              downloadPath: "C:/in"
          });
          //...and so on
          await page.click("#download-button");
          browser.close();
      })();
  </code></pre>
  
  <p><a href="https://github.com/bvn-architecture/cab_venn/blob/master/scraper.ts" rel="nofollow noreferrer">Full file here</a></p>
  
  <p>There is a <a href="https://github.com/microsoft/playwright/issues/853" rel="nofollow noreferrer">proposal for a better download</a> api in <a href="https://github.com/microsoft/playwright" rel="nofollow noreferrer">Playwright</a>, but I can't find the current API.
  There was a suggestion that <a href="https://chromedevtools.github.io/devtools-protocol/tot/Page/#event-downloadWillBegin" rel="nofollow noreferrer">something to do with the <code>downloadWillBegin</code> event</a> would be useful, but I've no idea how to access that from Playwright.</p>
  
  <p>I'm open to the suggestion that I should use Puppeteer instead, but I moved to playwright because I couldn't work out how to download a file with Pupeteer either, and <a href="https://github.com/puppeteer/puppeteer/issues/299" rel="nofollow noreferrer">the issue related to it suggested that the whole team had moved to Playwright</a>.</p>