How to download file with puppeteer using headless: true?

前端 未结 7 1692
攒了一身酷
攒了一身酷 2020-12-08 05:05

I\'ve been running the following code in order to download a csv file from the website http://niftyindices.com/resources/holiday-calendar:

7条回答
  •  旧巷少年郎
    2020-12-08 05:47

    I found a way to wait for browser capability to download a file. The idea is to wait for response with predicate. In my case URL ends with '/data'.

    I just didn't like to load file contents into buffer.

    await page._client.send('Page.setDownloadBehavior', {
        behavior: 'allow',
        downloadPath: download_path,
    });
    
    await frame.focus(report_download_selector);
    await Promise.all([
        page.waitForResponse(r => r.url().endsWith('/data')),
        page.keyboard.press('Enter'),
    ]);
    

提交回复
热议问题