Puppeteer中的多个浏览器与多个选项卡 [英] Multi browsers vs multi tabs in Puppeteer

查看:160
本文介绍了Puppeteer中的多个浏览器与多个选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有100个必须测试运行时错误的网页.我发现Puppeteer插件可以不费吹灰之力"地完成工作,但是我遇到了一个难题:一个启动的浏览器带有多个选项卡,或者每个链接都有一个新的浏览器.在这种情况下最好的方法是什么?

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?

在多个选项卡的情况下,我听说,有一个机会,CSS动画和别的东西(现在不记得)是行不通的时候标签是没有焦点.

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.

显然,多个浏览器会导致较高的CPU负载(否?)

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

推荐答案

这些是我认为每种方法的优点:

These are the advantages of each method in my opinion:

多个浏览器的优势:

  • 单独的进程:如果一个浏览器崩溃,则其他浏览器继续运行

一种浏览器(具有多个页面)的优势:

  • 较少的内存使用:尽管实际上内存和CPU使用在很大程度上取决于您正在执行的任务(屏幕截图,DOM操作)
  • cookie(和其他数据)已共享

还可以选择使用多个上下文,它比两个单独的浏览器需要更少的内存,但不共享cookie.

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

因此,实际上,您应该只尝试两种选择.您可能想看一看我写的库 puppteer-cluster 错误处理并在发生崩溃时重新启动浏览器.

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

只需将 CONCURRENCY_PAGE 替换为 CONCURRENCY_BROWSER ,即可尝试使用多个浏览器.您可能还想尝试第三个选项 CONCURRENCY_CONTEXT .

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.

这篇关于Puppeteer中的多个浏览器与多个选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆