Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer Hangs In Headless Mode

My Puppeteer script is running in headless mode and it's timing out.

I'm not sure exactly what's going wrong. The script runs fine locally, but when I'm running in headless mode it always times out.

I've read online that could be due to the script failing to load an external javascript source? Has anyone else run into this problem, and able to help out?

Here's my setup function for my Puppeteer instance:

 setUpPuppeteer: async () => {
    const headless = process.env.NODE_ENV === "production";
    const browser = await pupeteer.launch({
      headless,
      devtools: true,
      args: ['--no-sandbox' ]
    });
    const context = await browser.createIncognitoBrowserContext();
    const page = await context.newPage(); // Create new instance of puppet

    page.on('error', err => {
      logger.error('Puppeteer error.', err);
    });

    page.setDefaultNavigationTimeout(10000);

    if (process.env.NODE_ENV === 'production') {
      await page.setRequestInterception(true); // Optimize (no stylesheets, images)...
      page.on('request', request => {
        if (['image', 'stylesheet'].includes(request.resourceType())) {
          request.abort();
        } else {
          request.continue();
        }
      });
    }

    return {browser: context, page};
  },
like image 360
Harrison Cramer Avatar asked Jan 25 '20 19:01

Harrison Cramer


People also ask

How to enable execution in the headless mode in puppeteer?

This means if we are running a test using Puppeteer, then we won't be able to view the execution in the browser. To enable execution in the headed mode, we have to add the parameter: headless:false in the code. To begin, follow Steps 1 to 2 from the Chapter of Basic Test on Puppeteer, which are as follows −

How to execute the test in headless chromium using puppeteer?

By default, Puppeteer executes the test in headless Chromium. This means if we are running a test using Puppeteer, then we won't be able to view the execution in the browser. To enable execution in the headed mode, we have to add the parameter: headless:false in the code.

Why can't I view the execution of my test in puppeteer?

By default, Puppeteer executes the test in headless Chromium. This means if we are running a test using Puppeteer, then we won't be able to view the execution in the browser.

Does puppeteer time out on Ubuntu?

In an Ubuntu VM run using Vagrant, the script doesn’t time out but it does work a little slowly. (Both are on Node v8.9.2.) This may not be an issue with Puppeteer. I got the same timeouts with Chromy. I upgraded to Windows 10 x64 in the interim and had no issues whatsoever with Puppeteer. Sorry, something went wrong.


Video Answer


1 Answers

setUpPuppeteer: async () => {
    const headless = process.env.NODE_ENV === "production";
    const browser = await pupeteer.launch({
      headless: true,
      devtools: true,
      args: [
        '--ignore-certificate-errors',
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--disable-accelerated-2d-canvas',
        '--disable-gpu'
            ]
    });
    const context = await browser.createIncognitoBrowserContext();
    const page = await context.newPage(); // Create new instance of puppet

    page.on('error', err => {
      logger.error('Puppeteer error.', err);
    });

    page.setDefaultNavigationTimeout(10000);

    if (process.env.NODE_ENV === 'production') {
      await page.setRequestInterception(true); // Optimize (no stylesheets, images)...
      page.on('request', request => {
        if (['image', 'stylesheet'].includes(request.resourceType())) {
          request.abort();
        } else {
          request.continue();
        }
      });
    }

    return {browser: context, page};
  },
like image 186
Dadmand Avatar answered Oct 05 '22 13:10

Dadmand