How to pass userDataDir profile folder to Puppeteer
Asked Answered
I

3

13

I want to pass a custom profile to Puppeteer. To start I've tried to pass my real Google Chrome profile:

const browser = await puppeteer.launch({
  userDataDir: '/Users/[USERNAME]/Library/Application Support/Google/Chrome/Default',
  headless: false,
  slowMo: 250,
  ...
}

but when the browser opens if I go to Settings it shows Person 1 rather than the data from my Google Chrome profile

The userDataDir path above is what is shown in Profile Path when on Google Chrome I visit chrome://version (where [USERDATA] is my username)

I've tried also userDataDir: '~/Library/Application Support/Google/Chrome/Default'

I'm using
Puppeteer 0.11.0
Node 8.4.0
NPM 5.2.0
macOS El Capitan 10.11.6
MacBook Pro Retina, 15-inch, Mid 2015

Ingrowing answered 24/10, 2017 at 10:44 Comment(0)
I
21

Using a relative path worked. The path is relative to the folder where you are executing the cli command running the javascript that uses puppeteer:

await puppeteer.launch({
  userDataDir: './myUserDataDir',
})
Ingrowing answered 24/10, 2017 at 11:29 Comment(4)
I tried the solution it is giving the below error [9665:775:0117/220533.630257:ERROR:browser_dm_token_storage_mac.mm(153)] Error retrieving the machine serial number.Tapley
that didn't work! this still creating new temp profile.Parasite
try to play with path.resolve(__dirname, './myUserDataDir')Ingrowing
I still couldn't get it to work. Turns out the code I inherited included default args were sent to puppeteer. These default args already a default user-data-dir in that points to the temp folder, which came first so Chrome was using the temp dir even though I was specifying something different. I couldn't change the defaults around so the fix I found was to use userDataDir direct with puppeteer.launch, which takes precedence over the args. Using chrome://version was very useful debugging which profile directory is being used.Cultrate
G
1

Maybe a bit late, but there is a simple solution: you need to use back-tick quotes, because 'Application Support' includes a space.

const browser = await puppeteer.launch({
  userDataDir: `/Users/[USERNAME]/Library/'Application Support'/Google/Chrome/Default`,
  headless: false,
  slowMo: 250,
  ...
}

The code above will work

Gravitt answered 17/10, 2023 at 1:7 Comment(0)
B
0
await puppeteer.launch({
  userDataDir: 'myUserDataDir',
})

try using this, it will create a directory automatically.

Bluefish answered 18/8, 2023 at 8:18 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Sext
OP doesn't want to create a new directory. They're trying to use an existing Chrome profile.Electroshock

© 2022 - 2024 — McMap. All rights reserved.