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.

The `page.setCookie` method should also accept raw cookies

See original GitHub issue

Steps to reproduce

Tell us about your environment:

  • Puppeteer version: 1.12.2
  • Platform / OS version: macOS 10.14.3
  • URLs (if applicable):
  • Node.js version: 10.15.0

What steps will reproduce the problem?

await page.setCookie('foo=bar');

What is the expected result?

It works.

What happens instead?

It doesn’t.


Not being able to set raw cookies is very inconvenient. There are multiple situations where it would be useful:

  1. Copy-pasting cookies from DevTools.
  2. Accepting cookies as user input in a command-line tool.
  3. Getting cookies from a server request, for example in Express where you read the cookie header, and then pass them to Puppeteer.

Yes, I could find an npm package for this, but the ones I found are not very good (tough-cookie included). I also think it’s such a basic thing that a big percentage of Puppeteer users will need, that it should be supported by Puppeteer natively.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:11
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
ultroxcommented, Sep 5, 2020

Hello, there googler!!

I used the ‘EditThisCookie’ extension that has a nice feature export, it will export cookies as JSON, in a perfect format so the puppeteer can consume without any transformation.

image

const puppeteer = require('puppeteer');
const cookies = require('./cookies.json')

async function main() {
 const browser = await puppeteer.launch()
 const page = await browser.newPage()
 await page.setCookie(...cookies)
	
 await page.goto('https://www.reddit.com')
 await page.screenshot({ path: 'reddit.png' })
 await browser.close()
}

main().catch(console.error)
1reaction
aslushnikovcommented, Mar 4, 2019

@sindresorhus in this case page.evaluateOnNewDocument would probably work as a workaround.

But I guess what you really want is a browserContext.setCookie(url, 'foo=bar') method.

Read more comments on GitHub >

github_iconTop Results From Across the Web

json - Passing multiple cookies to puppeteer - Stack Overflow
There is only page.setCookie method which allows setting only one cookie at a time. · But it contains a rest and spread operator...
Read more >
setcookie - Manual - PHP
setcookie () defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent...
Read more >
Page.setCookie() method - Puppeteer
On this page. Page.setCookie() method. Signature:​. class Page { setCookie(...cookies: Protocol.Network.CookieParam[]): Promise<void>; }. Parameters​ ...
Read more >
Using HTTP cookies - MDN Web Docs
After receiving an HTTP request, a server can send one or more Set-Cookie headers with the response. The browser usually stores the cookie...
Read more >
A Complete Guide to Working With Cookies in Go
Cookies can be written in a HTTP response using the http.SetCookie() function, and read from a HTTP request using the *Request.Cookie() method.
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