behindClouds
behindClouds

Reputation: 101

Cannot download file while headless: true, works when headless: false [Puppeteer]

Im running a script that logs into an authenticated session on a website and clicks a button to download an excel file. Im able to run it with no problems while headless: false, but when headless:true, the file does not download.

My research suggests that the browser is closing before the download completes possibly? Ive added a wait of about 15 seconds, which is much longer than it should need to download the file, but still not getting anything. Another solution I tried was manually removing the HeadlessChrome substring from the userAgent in case the site was blocking it, but that didnt work either. Is it okay to use headless:false in a script that is used in a production web application deployed on Heroku?

async function getData () {
  try {
    const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms))
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
 
    await page.goto('<url>');
    //login
    await page.type('#username',username);
    await page.click('#signIn');
    await wait(4000)
    await page.type('#password',password);
    await page.click('#signIn');
   
    await page.waitForNavigation();
    await page.keyboard.press('Enter'); //click out of any pop up
    // //go to merchandising page
    await page.click('#m_69-link');
    await page.waitForSelector('#ExcelReportButton', {visible: true})
    //click on export as excel icon
    await wait(4000)
    await page.click('#ExcelReportButton');
    await wait(15000)
    await browser.close();
  } catch (error) {
    console.log(error)
  }
};

Upvotes: 2

Views: 1396

Answers (1)

Blagoj Petrov
Blagoj Petrov

Reputation: 295

try by adding additional headers, it worked for me:

await page.setExtraHTTPHeaders({
        'Accept-Language': 'en-US,en;q=0.9'
    });
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36');

Upvotes: 1

Related Questions