[英]When using puppeteer, is it faster to open a new page after launching the browser, or should I try and use the current tab that gets loaded
Here's my current code:这是我当前的代码:
const browser = await puppeteer.launch({...});
const page = await browser.newPage();
and then i do all my puppeteer stuff.然后我做我所有的木偶戏。 The issue is, this opens 2 tabs: one when the browser gets launched, and one when you call the newpage function.
问题是,这会打开 2 个选项卡:一个是在浏览器启动时,另一个是在您调用 newpage 函数时。 Would it be faster/worthwhile to do this instead?
这样做会更快/值得吗?
const browser = await puppeteer.launch({...});
const getPages = await browser.pages();
const page = getPages[0];
This loads the browser and accesses the tab that is already open.这将加载浏览器并访问已打开的选项卡。 Is this better practice or does it not really make a difference?
这是更好的做法还是真的没有什么不同?
我更喜欢使用第一页而不是打开一个新页面,这比打开一个新页面要快,但我认为这并不能真正提高性能和速度。
const [ page ] = await browser.pages();
If you are loading multiple pages it is recommended to use the context
API (see here for docs ):如果您要加载多个页面,建议使用
context
API(请参阅此处的文档):
// Create a new incognito browser context
const context = await browser.createIncognitoBrowserContext();
// Create a new page inside context.
const page = await context.newPage();
// ... do stuff with page ...
await page.goto('https://example.com');
// Dispose context once it's no longer needed.
await context.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.