Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer get 3rd-party cookies

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

For first party, I know I can use:

await page.cookies()
like image 383
Piotr Wójcik Avatar asked May 09 '18 12:05

Piotr Wójcik


People also ask

How do you get all the cookies in puppeteer?

setCookie() method in puppeteer to set cookies for any website. Remember, you need to first navigate to a page via Puppeteer before you can set cookies on a particular website. So, always use the page. goto() first and then set cookies.

Is puppeteer a headless browser?

Puppeteer is a Node library which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. It can also be configured to use full (non-headless) Chrome or Chromium.


1 Answers

You can create a Chrome DevTools Protocol session on the page target using target.createCDPSession(). Then you can send Network.getAllCookies to obtain a list of all browser cookies.

The page.cookies() function will only return cookies for the current URL. So we can filter out the current page cookies from all of the browser cookies to obtain a list of third-party cookies only.

const client = await page.target().createCDPSession();
const all_browser_cookies = (await client.send('Network.getAllCookies')).cookies;
const current_url_cookies = await page.cookies();
const third_party_cookies = all_browser_cookies.filter(cookie => cookie.domain !== current_url_cookies[0].domain);

console.log(all_browser_cookies); // All Browser Cookies
console.log(current_url_cookies); // Current URL Cookies
console.log(third_party_cookies); // Third-Party Cookies
like image 73
Grant Miller Avatar answered Oct 13 '22 06:10

Grant Miller