简体   繁体   English

使用 puppeteer 时,启动浏览器后打开新页面是否更快,或者我应该尝试使用当前加载的选项卡

[英]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.

相关问题 打开新选项卡后,如何切换到 Puppeteer 浏览器实例中的上一个选项卡? - How can I switch to a previous tab in a Puppeteer browser instance after opening a new tab? 当我尝试在XHTML中使用js打开新的浏览器窗口时,该窗口无法打开 - when I try to open a new browser window using js in XHTML, the window does not open 当仅使用xul加载当前页面的页面时,如何添加事件侦听器? - How can I add event listener when the page loaded using xul for the current tab only? 打开页面新选项卡并刷新当前页面 - open page new tab and refresh current page 当我使用浏览器操作Javascript打开新标签页时,Google Chrome关闭,不知道为什么 - Google Chrome closes when I open a new tab using my browser action Javascript, no idea why 当我使用 JS 打开一个新选项卡时,会保留一些浏览器历史记录。 如何删除它? - When I open a new tab using JS, some browser history is kept. How to remove it? 在新标签页中加载页面后调用函数 - Call function after page was loaded in new tab 如何在浏览器的新标签页中打开页面? - How to open page in a new tab in browser? 如何使用量角器和Chrome浏览器打开新标签页 - How can i open a new tab using protractor and Chrome browser 在新标签页中打开同一页面,但在当前标签页中打开新链接 - Open same page in new tab but new link if in current tab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM