question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

set a limit on pages number per browser

See original GitHub issue

Hi,

Tell us about your environment:

  • Puppeteer version: 1.11.0
  • Platform / OS version: Centos 7
  • URLs (if applicable):
  • Node.js version: v10.14.2

Is there a way to en n pages in a browser and limit the page number by a maxNbrePage parameter.

I have a long list of url to open, and I would like to open them but with maximum of 3 pages at the same time. and repace finished or loaded page by a new page in the list.

I tested with this code, it works, but without a limit on the pages number at time.

'use strict';

const puppeteer = require('puppeteer');

async function processPage(browser, url) {
	console.log("processPage start:", url);
	const page = await browser.newPage();
    const response = await page.goto(url, {
                timeout: 3000000
            });
	await page.close();
	console.log("processPage end:", url);
	return {page:response};
}

(async function main() {
	
	
  try {
    const browser = await puppeteer.launch({
		headless: true,
		args: ['--no-sandbox'],
		userDataDir: `${__dirname}/profile-dir`
    });
	
	var requests = ['http://example.org/', 'https://www.example.com/', 'http://www.example.com/', 'http://www.example.org/','http://www.example.org/', 'http://www.example.com/', 'http://www.example.com/', 'http://www.example.org/'];	
	
	var response_promises = [];
	
	requests.forEach(function(request) {
		response_promises.push(processPage(browser, request));
	});
	
	var responses = await Promise.all(response_promises);
	
    await browser.close();

  } catch (err) {
    console.error(err);
  }
})();

Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
VirtumartOzcommented, Sep 20, 2020

‘use strict’;

const puppeteer = require(‘puppeteer’);

async function processPage(browser, url) { console.log(“processPage start:”, url); const page = await browser.newPage(); const response = await page.goto(url, { timeout: 3000000 }); await page.close(); console.log(“processPage end:”, url); return {page:response}; }

(async function main() {

try { const browser = await puppeteer.launch({ headless: true, args: [‘–no-sandbox’], userDataDir: ${__dirname}/profile-dir });

var requests = ['http://example.org/', 'https://www.example.com/', 'http://www.example.com/', 'http://www.example.org/','http://www.example.org/', 'http://www.example.com/', 'http://www.example.com/', 'http://www.example.org/'];	

var response_promises = [];
for(let i = 0; i < requests.length; i ++){
      if( i > 0 && (i % 3 == 0 || (i == requests.length - 1)) {
        var responses = await Promise.all(response_promises);
        responses_promises = [];
     }
     response_promises.push(processPage(browser, requests[i]));
}
await Promise.all(response_promises); // for the last unprocessed item
await browser.close();

} catch (err) { console.error(err); } })();

1reaction
saqib-ahmedcommented, Feb 20, 2020

If it helps anyone, you can use the example from the accepted answer in the following StackOverflow post to limit the number of pages per browser: https://stackoverflow.com/questions/50557418/puppeteer-opening-chrome-instance-for-each-file-at-once

Read more comments on GitHub >

github_iconTop Results From Across the Web

Limit - Set Limits for Distracting Sites
To use Limit, simply enter the websites you find distracting and choose a daily time limit. For example, you could limit yourself to...
Read more >
How to use Limit | Freedom Help Center
Using Limit​​ To use Limit, simply click on the icon, which is usually located at the top right of your browser window. Then...
Read more >
Browser limitation with maximum Page length - Stack Overflow
I googled and found that the limit of a web page should be 10-30KB but in the same project we have pages with...
Read more >
Blocking: The Web Performance Villain - The Blue Triangle Blog
So depending on what browser is used, the limit on the number of connections per host will range anywhere from 2 to 13,...
Read more >
Set time limits for apps and websites in Screen Time on Mac
Limit a website: Click the arrow next to the Websites category at the bottom of the list. If the website has been visited,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found