Puppeteer POST request and file download

Puppeteer send POST request, wait for response and clickable selector and download file. You can use the data from the 'response' to download specific file based on it instead of waiting for the '#download' selector to appear.

const puppeteer = require("puppeteer");

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  const navigationPromise = page.waitForNavigation({waitUntil: 'domcontentloaded'});

  await page.goto("http://localhost/puppeteer/");
  await navigationPromise;

  page.on("request", interceptedRequest => {
    console.log('Request');
    interceptedRequest.continue({
      method: "POST",
      postData: '{"query":{"object":"custom"}}',
      headers: {
        ...interceptedRequest.headers(),
        "Content-Type": "application/json; charset=utf-8",
      },
    });
  });

  await page.goto("http://localhost/puppeteer/export.php");
  await navigationPromise;

  await page.waitForSelector('#download', {timeout: 300000});
  await page._client.send('Page.setDownloadBehavior', {
    behavior: 'allow',
    downloadPath: 'logs'
  });
  await page.click('#download');

  await browser.close();
})();