24

By doing what user Md. Abu Taher suggested, i used a plugin called EditThisCookie to download the cookies from my browser.

The exported cookies are in JSON format, in fact it is an array of objects.

Is it possible to pass this array as a parameter to puppeteer? Can i pass an array of objects to page.setCookies() function?

user1584421
  • 3,499
  • 11
  • 46
  • 86
  • There is only [page.setCookie](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetcookiecookies) method which allows setting only one cookie at a time. – Vaviloff May 29 '18 at 16:43
  • But it contains a rest and spread operator so i think you can pass an array of cookies. – user1584421 May 30 '18 at 09:21
  • Fair enough, my bad. [Looking at the code](https://github.com/GoogleChrome/puppeteer/blob/32f4c173c818a023d8ae24ecde78c5f3fc57a0c1/lib/Page.js#L345) it can indeed accept an array of objects. So I guess you just need to make sure your saved cookies conform to the [Chrome cookie specs](https://chromedevtools.github.io/devtools-protocol/tot/Network#type-CookieParam) – Vaviloff May 30 '18 at 09:38
  • What if the cookies i want to pass does not conform to these specs? I found some cookies that have attributes like (domain, hostOnly, session, storeId, id) that are not in the specs. Also sameSite is "no_restriction" while puppeteer wants 'Strict' or 'Lax'. – user1584421 May 30 '18 at 12:53

3 Answers3

39

You can use spread syntax await page.setCookie(...cookies);, where cookies is an array of cookie objects. https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetcookiecookies

Try it on https://try-puppeteer.appspot.com/

const browser = await puppeteer.launch();
const url = 'https://example.com';
const page = await browser.newPage();
await page.goto(url);

const cookies = [{
  'name': 'cookie1',
  'value': 'val1'
},{
  'name': 'cookie2',
  'value': 'val2'
},{
  'name': 'cookie3',
  'value': 'val3'
}];

await page.setCookie(...cookies);
const cookiesSet = await page.cookies(url);
console.log(JSON.stringify(cookiesSet));
await browser.close();
lima_fil
  • 1,744
  • 27
  • 34
  • Did you try it on https://try-puppeteer.appspot.com/? `async` wrapper with puppeteer is already provided there. – lima_fil May 30 '18 at 11:56
  • 3
    Also `page.cookies()` is for reading cookies from specific `url` https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagecookiesurls, `page.setCookie(...cookies)` is for setting cookies. – lima_fil May 30 '18 at 11:57
  • 3
    @lima_fil you need to add the domain or ULR value in your array for your answer to work: const cookies = [{ name: 'cookie1', value: 'val1', domain: '.domain1' },{ name: 'cookie2', value: 'val2', domain: '.domain2' },{ name: 'cookie3', value: 'val3', domain: '.domain3' }]; – Nicolas Bouvrette Nov 04 '18 at 14:30
21

You can call page.setCookie() with a spread operator to set multiple cookies at once.

However, make sure you call it before calling page.goto(url) because if you call it afterwards, the cookies will be set after the page has been loaded.

Calling page.setCookie() before page.goto(url) will require you to add a domain key to each cookie.

const cookies = [
  {name: 'cookie1', value: 'val1', domain: 'example.com'},
  {name: 'cookie2', value: 'val2', domain: 'example.com'},
  {name: 'cookie3', value: 'val3', domain: 'example.com'},
];

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setCookie(...cookies);
await page.goto('https://example.com');
await browser.close();
Yoav Kadosh
  • 4,807
  • 4
  • 39
  • 56
0

StorageAce chrome extension provides support for exporting cookies directly in Puppeteer format which conforms to the Chrome cookie specs.You can configure it in settings as below:

enter image description here

Then copy cookies to clipboard:

enter image description here

CodeZila
  • 919
  • 2
  • 14
  • 31