Puppeteer get 3rd-party cookies

Piotr Wójcik picture Piotr Wójcik · May 9, 2018 · Viewed 9k times · Source

How can I get 3rd-party cookies from a website using Puppeteer?

For first party, I know I can use:

await page.cookies()

Answer

Vaviloff picture Vaviloff · May 11, 2018

I was interested to know the answer so have found a solution too, it works for the current versions of Chromium 75.0.3765.0 and puppeteer 1.15.0 (updated May 2nd 2019).

Using internal puppeteer page._client methods we can make use of Chrome DevTools Protocol directly:

(async() => {
  const browser = await puppeteer.launch({});
  const page = await browser.newPage();
  await page.goto('https://stackoverflow.com', {waitUntil : 'networkidle2' });

  // Here we can get all of the cookies
  console.log(await page._client.send('Network.getAllCookies'));

})();

In the object returned there are cookies for google.com and imgur.com which we couldn't have obtained with normal browser javascript:

3d-party cookies!