I\'ve been running the following code in order to download a csv file from the website http://niftyindices.com/resources/holiday-calendar:
setDownloadBehavior works fine for headless: true mode, and file is eventually downloaded, but throws an exception when finished, so for my case a simple wrapper helps to forget about this issue and just gets the job done:
const fs = require('fs');
function DownloadMgr(page, downloaddPath) {
if(!fs.existsSync(downloaddPath)){
fs.mkdirSync(downloaddPath);
}
var init = page.target().createCDPSession().then((client) => {
return client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: downloaddPath})
});
this.download = async function(url) {
await init;
try{
await page.goto(url);
}catch(e){}
return Promise.resolve();
}
}
var path = require('path');
var DownloadMgr = require('./classes/DownloadMgr');
var downloadMgr = new DownloadMgr(page, path.resolve('./tmp'));
await downloadMgr.download('http://file.csv');