question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trouble using session/cookies through userDataDir?

See original GitHub issue

Steps to reproduce

Hey all! Not sure at all if this is possible but wanted to be sure. Just wanted to clarify. I’m using userDataDir to point to my Default and active profile. But with headless: false I’ve attempted loading a variety of sites and none show me as logged in.

The docs don’t necessarily clarify either way, nor did a quick search.

What steps will reproduce the problem?

import pup from 'puppeteer'

async function run() {
  const browser = await pup.launch({
    userDataDir: '~/Library/Application Support/Google/Chrome/Default',
    headless: false,
  })
  const page = await browser.newPage()
  await page.goto('https://facebook.com')
  await browser.close()
}

try {
  run()
} catch (err) {
  console.error(err)
}

What is the expected result?

Uses the session from my regular Chrome.

What happens instead?

No session.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:10
  • Comments:29 (11 by maintainers)

github_iconTop GitHub Comments

9reactions
MrXyfircommented, Oct 18, 2018

Any (reasonable) solution to this yet? I think I’ve tried every possible suggestion and nothing has worked. Is the only option manually saving and injecting cookies?

// Saving the cookies

const client = await page.target().createCDPSession();
// This gets all cookies from all URLs, not just the current URL
const cookies: puppeteer.Cookie[] = (await client.send(
  'Network.getAllCookies'
))['cookies'];

console.log('Saving', cookies.length, 'cookies');
await fs.writeJSON(cookiesPath, cookies);
// Loading the cookies

try {
  const cookies: puppeteer.Cookie[] = await fs.readJSON(cookiesPath);
  console.log('Loading', cookies.length, 'cookies into browser');
  await page.setCookie(...cookies);
} catch (err) {}

Note that fs is actually fs-extra.

6reactions
MrXyfircommented, Mar 2, 2019

@entrptaher Yep, I encountered those issues. There is no good solution as far as I’m aware. Any solution would be a fragile hack. Pretty much you’d have to do the same thing as with cookies: save them elsewhere and restore the data when needed. However this is not a simple task by any means because you cannot easily load and set all of the local storage data for all domains at once like you can with cookies. You would need a frame open to a URL of the same origin you wish to manage the local storage of. Ultimately it wasn’t worth the effort (for me at least) to write a similar system as I had with cookies.

I think the best solution is to keep complaining until the Chrome and/or Chrome DevTools Protocol developers fix this. If I remember correctly, the issue isn’t really with puppeteer, since it mostly is just a wrapper for the DevTools protocol.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to manage log in session through headless chrome?
My problem is that every new instance of headless browser clears my login session, and then I need to login again and again......
Read more >
PSA user-data-dir structure is changing - Google Groups
We are changing headless to use a "Default" profile, this means that we share the same structure as non-headless Chrome Default profile, ...
Read more >
Session Cookie Login Problem Fix for All Major Browsers
1) Locate the Internet Settings page. The easiest way to do that is to click on the three bars icon in the top...
Read more >
Microsoft Edge Browser Policy Documentation
These tables list all of the browser-related group policies available in this release of Microsoft Edge. Use the links in the table to...
Read more >
Hands in the Cookie Jar: Dumping Cookies with Chromium's ...
This is great for attackers since they can start Chromium with a debug port completely transparent to the user. --user-data-dir=/path/to/ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found