最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to use user-data-dir but for multiple puppeteer windows - Stack Overflow

programmeradmin0浏览0评论

So what I am trying to do is to open puppeteer window with my google profile, but what I want is to do it multiple times, what I mean is 2-4 windows but with the same profile - is that possible? I am getting this error when I do it:

(node:17460) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
[45844:13176:0410/181437.893:ERROR:cache_util_win(20)] Unable to move the cache: Access is denied. (0x5)
const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    headless:false,
    '--user-data-dir=C:\\Users\\USER\\AppData\\Local\\Google\\Chrome\\User Data',
  );
  const page = await browser.newPage();
  await page.goto('');
  await page.screenshot({ path: 'example.png' });
        
  await browser.close();
})();

So what I am trying to do is to open puppeteer window with my google profile, but what I want is to do it multiple times, what I mean is 2-4 windows but with the same profile - is that possible? I am getting this error when I do it:

(node:17460) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
[45844:13176:0410/181437.893:ERROR:cache_util_win(20)] Unable to move the cache: Access is denied. (0x5)
const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    headless:false,
    '--user-data-dir=C:\\Users\\USER\\AppData\\Local\\Google\\Chrome\\User Data',
  );
  const page = await browser.newPage();
  await page.goto('https://example.');
  await page.screenshot({ path: 'example.png' });
        
  await browser.close();
})();
Share Improve this question edited Apr 17, 2021 at 11:54 theDavidBarton 8,8714 gold badges31 silver badges56 bronze badges asked Apr 10, 2021 at 15:22 DUMBUSERDUMBUSER 5318 silver badges25 bronze badges 5
  • 3 You have syntax problems in that launch object – charlietfl Commented Apr 10, 2021 at 15:26
  • i just wrote a quick one since mine is huge, it's just an example – DUMBUSER Commented Apr 10, 2021 at 15:28
  • 1 Ok. but people will focus on that because lots and lots of questions here are just syntax typos. At least make it syntactically correct to avoid any confusion. You can edit the question at any time to improve or clarify it – charlietfl Commented Apr 10, 2021 at 15:30
  • 2 Also double check that the error actually still is the same with the shortened sample code you show. – Tomalak Commented Apr 13, 2021 at 8:47
  • i tried it that away, i can't even open it if the main browser is opened – DUMBUSER Commented Apr 13, 2021 at 10:07
Add a ment  | 

1 Answer 1

Reset to default 7 +50

Note: It is already pointed in the ments but there is a syntax error in the example. The launch should look like this:

const browser = await puppeteer.launch({
  headless: false,
  args: ['--user-data-dir=C:\\Users\\USER\\AppData\\Local\\Google\\Chrome\\User Data']
});

The error is ing from the fact that you are launching multiple browser instances at the very same time hence the profile directory will be locked and cannot be moved to reuse by puppeteer.

You should avoid starting chromium instances with the very same user data dir at the same time.

Possible solutions

  • Make the opened windows sequential, can be useful if you have only a few. E.g.:
const firstFn = async () => await puppeteer.launch() ...
const secondFn = async () => await puppeteer.launch() ...

(async () => {
  await firstFn()
  await secondFn()
})();
  • Creating copies of the user-data-dir as User Data1, User Data2 User Data3 etc. to avoid conflict while puppeteer copies them. This could be done on the fly with Node's fs module or even manually (if you don't need a lot of instances).
  • Consider reusing Chromium instances (if your use case allows it), with browser.wsEndpoint and puppeteer.connect, this can be a solution if you would need to open thousands of pages with the same user data dir.
    Note: this one is the best for performance as only one browser will be launched, then you can open as many pages in a for..of or regular for loop as you want (using forEach by itself can cause side effects), E.g.:
const puppeteer = require('puppeteer')
const urlArray = ['https://example.', 'https://google.']

async function fn() {
  const browser = await puppeteer.launch({
    headless: false,
    args: ['--user-data-dir=C:\\Users\\USER\\AppData\\Local\\Google\\Chrome\\User Data']
  })
  const browserWSEndpoint = await browser.wsEndpoint()

  for (const url of urlArray) {
    try {
      const browser2 = await puppeteer.connect({ browserWSEndpoint })
      const page = await browser2.newPage()
      await page.goto(url) // it can be wrapped in a retry function to handle flakyness

      // doing cool things with the DOM
      await page.screenshot({ path: `${url.replace('https://', '')}.png` })
      await page.goto('about:blank') // because of you: https://github./puppeteer/puppeteer/issues/1490
      await page.close()
      await browser2.disconnect()
    } catch (e) {
      console.error(e)
    }
  }
  await browser.close()
}
fn()
发布评论

评论列表(0)

  1. 暂无评论