How can I get 3rd-party cookies from a website using Puppeteer?
For first party, I know I can use:
await page.cookies()
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With