Multi browsers vs multi tabs in Puppeteer

萝らか妹 提交于 2021-01-01 04:56:06

问题


I have 100 web pages that I have to test for runtime errors. I found the Puppeteer plugin that can do that "no sweat", but I ran into one dilemma: have one launched browser with multiple tabs or new browser for each link. What is the best approach in this case?

In case of multiple tabs, I heard, there is a chance that css animation and something else (can't remember now) would not work when tab is not in focus.

Multiple browser, obviously, causes the higher CPU load (no?)


回答1:


These are the advantages of each method in my opinion:

Advantage of multiple browsers:

  • Separate processes: If one browser crashes, the other browsers keep running

Advantage of one browser (with multiple pages):

  • Less memory usage: Although in reality memory and CPU usage depends to a high degree on the task you are doing (screenshot, DOM manipulation)
  • The cookies (and other data) are shared

There is also the option to use multiple contexts, which needs less memory than two separate browsers, but does not share cookies.

So in reality, you should probably just give both options a try. You might want to have a look at the library puppteer-cluster I wrote, which also takes care of error handling and browser restarting in case of crashes.

You can just write your code and switch between multiple browsers vs multiple pages with just one line:

const cluster = await Cluster.launch({
    concurrency: Cluster.CONCURRENCY_PAGE, // which kind of concurrency
    maxConcurrency: 2, // number of parallel workers
});

Just replace CONCURRENCY_PAGE with CONCURRENCY_BROWSER to give multiple browser a try. There is also a third option CONCURRENCY_CONTEXT you might want to try.



来源:https://stackoverflow.com/questions/52227893/multi-browsers-vs-multi-tabs-in-puppeteer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!