Here's a function that will wait for a download to finish once its
triggered in pupepeteer.
1
2
3
4
5
6
7
8
9
10
11
12
13
| import puppeteer from "puppeteer"
async function waitUntilDownload(page, fileName = '') {
return new Promise((resolve, reject) => {
page._client().on('Page.downloadProgress', e => { // or 'Browser.downloadProgress'
if (e.state === 'completed') {
resolve(fileName);
} else if (e.state === 'canceled') {
reject();
}
});
});
}
|
Here is is in an example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| (async () => {
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
})
const page = await browser.newPage()
page.target().createCDPSession().then((client) => {
return client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: '/tmp'})
});
console.log( "Loading download page" );
await page.goto(
'https://afdc.energy.gov/fuels/electricity_locations.html#/analyze?fuel=ELEC',
{ waitUntil: 'networkidle0' }
);
// Query for an element handle.
const element = await page.waitForSelector('a.afdc-btn');
console.log( "Starting download" )
await element.click();
console.log( "Waiting for download to complete" );
await waitUntilDownload( page );
console.log( "Download complete" )
await browser.close();
})()
|