javascript - Puppeteer Hangs In Headless Mode - Stack Overflow

My Puppeteer script is running in headless mode and it's timing out.I'm not sure exactly what

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};
  },

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};
  },
Share Improve this question edited Jan 25, 2020 at 19:50 Harrison Cramer asked Jan 25, 2020 at 19:13 Harrison CramerHarrison Cramer 4,53611 gold badges39 silver badges74 bronze badges 1
  • if devtools is true, the headless option will get set to false. set the devtools to false and try again. – mbit Commented Jan 26, 2020 at 2:32
Add a ment  | 

1 Answer 1

Reset to default 4
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};
  },

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744227308a4564071.html

相关推荐

  • javascript - Puppeteer Hangs In Headless Mode - Stack Overflow

    My Puppeteer script is running in headless mode and it's timing out.I'm not sure exactly what

    8天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信