16

I want to use my local user's profile with Puppeteer. However, it doesn't seem to work.

I launch it with these args.

const browser = await puppeteer.launch({
    executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
    userDataDir: '/Users/me/Library/Application Support/Google/Chrome',
});

When headless, it doesn't use the user's local profile's cookies at all, even though I'd expect it to. When it isn't headless, it can't even open the tab; Puppeteer crashes with

(node:23303) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!


TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md

Is there a way to use my local user's profile? I'm using ^1.7.0 and Chrome 70.0.3521.2.

Puppeteer user
  • 171
  • 2
  • 5
  • I have also been looking for the solution to this. It appears the only current working solution is to inject cookies which is overly complicated. – user1768079 Nov 29 '18 at 14:07

2 Answers2

6

Rather than setting a userDataDir path in the Puppeteer.launch arguments you can use the chrome-cookies-secure NPM package to use cookies for one of your existing Chrome Profiles. This solution does not require Chrome Canary to be installed.

With your macOS keychain authorisation, the package reads the cookies for a given url from your hard-disk and makes them accessible in NodeJS. You can then load them into Puppeteer using the page.setCookie(...) method.

Here's an example:

const chrome = require('chrome-cookies-secure');
const puppeteer = require('puppeteer');

const url = 'https://www.yourUrl.com';

const getCookies = (callback) => {
    chrome.getCookies(url, 'puppeteer', function(err, cookies) {
        if (err) {
            console.log(err, 'error');
            return
        }
        console.log(cookies, 'cookies');
        callback(cookies);
    }, 'yourProfile') // e.g. 'Profile 2'
}

// find profiles at ~/Library/Application Support/Google/Chrome

getCookies(async (cookies) => {
    const browser = await puppeteer.launch({
        headless: false
    });
    const page = await browser.newPage();

    await page.setCookie(...cookies);
    await page.goto(url);
    await page.waitFor(1000);
    browser.close()
});
Reece Daniels
  • 1,147
  • 12
  • 16
5

I solved this on MacOS by installing chrome canary, copying my default folder contained in ~/Library/Application Support/Google/Chrome/Default to ~/Library/Application Support/Google/Chrome\ Canary/Default

My working code looks like this:

async function run() {
  const browser = await puppeteer.launch({
    headless: false, 
    executablePath: '/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary',
    userDataDir: '/Users/radium/Library/Application\ Support/Google/Chrome\ Canary/',
  });
}

I was previously using the file path all the way to the Default folder, and truncated it to end with 'Chrome Canary' folder. This fixed everything. I have not tried with regular chrome.

Alejandro Vales
  • 2,816
  • 22
  • 41
user1768079
  • 683
  • 3
  • 10
  • 18
  • I apologize for the poor formatting. I haven't posted many answers on stackoverflow and am not sure how to get the formatting right... I just copy pasted from my editor. – user1768079 Nov 30 '18 at 05:39