Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Puppeteer use local profile's cookies

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.

like image 328
Puppeteer user Avatar asked Aug 15 '18 19:08

Puppeteer user


People also ask

How do I enable cookies on my puppeteer?

you can use page. cookies() to get all the cookies on the current page so you can check for yourself if they are set or not.

Does puppeteer use cookies?

Takeaways. We can use cookies and Web Storage APIs through Playwright and Puppeteer to set test state and speed up test suites.

Can puppeteer use Chrome instead of Chromium?

Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium.

How do you connect already existing Chrome browser with puppeteer?

You can connect to an existing using the connect function: const browserURL = 'http://127.0.0.1:21222'; const browser = await puppeteer. connect({browserURL}); But, if you want to use those 2 lines you need to launch Chrome with the "--remote-debugging-port=21222 argument.


1 Answers

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()
});
like image 151
Reece Daniels Avatar answered Sep 21 '22 05:09

Reece Daniels