Search code examples
jsonnode.jscookiespuppeteer

Passing multiple cookies to puppeteer


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?


Solution

  • 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();