Search code examples
javascriptnode.jspuppeteerchromiumfreeze

Puppeteer not working as excepted (pause/freeze when browser not in focus)


I'm using Puppeteer.js to crawl some URL. I'm using the default Chromium browser of Puppeteer.
All is working well, but the problem is, that when I run the crawling script, and doing other things in the background and the focus is no longer on the Chromium browser of Puppeteer, it's not working: waiting for elements way too long, and abort operations, or in other words: puppeteer is paused (or freeze).

P.S, I'm also using puppeteer-extra and puppeteer-extra-plugin-stealth NPM packages for advance options.

Here is how I create the browser and the page:

async initiateCrawl(isDisableAsserts) {
    // Set the browser.
    this.isPlannedClose = false;
    const browser = await puppeteerExtra.launch({
        headless: false,
        args: [
            '--no-sandbox',
            '--disable-setuid-sandbox',
            '--disable-dev-shm-usage',
            '--start-maximized',
            '--disable-background-timer-throttling',
            '--disable-backgrounding-occluded-windows',
            '--disable-renderer-backgrounding'
        ]
    });
    const pid = browser.process().pid;
    browser.on('disconnected', () => {
        systemUtils.killProcess(pid);
        if (!this.isPlannedClose) {
            systemUtils.exit(Status.BROWSER_CLOSE, Color.RED, 0);
        }
    });
    process.on('SIGINT', () => {
        this.close(browser, true);
    });
    // Set the page and close the first empty tab.
    const page = await browser.newPage();
    const pages = await browser.pages();
    if (pages.length > 1) {
        await pages[0].close();
    }
    await page.setRequestInterception(true);
    await page.setJavaScriptEnabled(false);
    await page.setDefaultNavigationTimeout(this.timeout);
    page.on('request', (request) => {
        if (isDisableAsserts && ['image', 'stylesheet', 'font', 'script'].indexOf(request.resourceType()) !== -1) {
            request.abort();
        } else {
            request.continue();
        }
    });
    return {
        browser: browser,
        page: page
    };
}

I already looked at:
https://github.com/puppeteer/puppeteer/issues/3339
https://github.com/GoogleChrome/chrome-launcher/issues/169
https://www.gitmemory.com/issue/GoogleChrome/puppeteer/3339/530620329

Not working solutions:

const session = await page.target().createCDPSession();
await session.send('Page.enable');
await session.send('Page.setWebLifecycleState', {state: 'active'});
const chromeArgs = [
  '--disable-background-timer-throttling',
  '--disable-backgrounding-occluded-windows',
  '--disable-renderer-backgrounding'
];
var ops = {args:[
        '--kiosks',
        '--disable-background-timer-throttling',
        '--disable-backgrounding-occluded-windows',
        '--disable-renderer-backgrounding',
        '--disable-canvas-aa',
        '--disable-2d-canvas-clip-aa',
        '--disable-gl-drawing-for-tests',
        '--disable-dev-shm-usage', 
        '--no-zygote', 
        '--use-gl=desktop', 
        '--enable-webgl',
        '--hide-scrollbars',
        '--mute-audio',
        '--start-maximized',
        '--no-first-run',
        '--disable-infobars',
        '--disable-breakpad', 
        '--user-data-dir='+tempFolder, 
        '--no-sandbox',
        '--disable-setuid-sandbox'
    ], headless: false, timeout:0   };
puppeteer = require('puppeteer');
    browser = await puppeteer.launch(ops);
    page = await browser.newPage();

Has anyone faced this issue before and have any idea how to solve this? Thanks.


Solution

  • My issue was solved when I updated to the latest puppeteer version (9.0.0).