I am using puppeteer to crawl sites, take screenshots and log all the set cookies.
Everything is working fine but only first party cookies are saved to the browser. After some tweaking I found out that it is probably the setting in the attached picture that I have to change to "Allow all cookies".
I couldn't find any way to change the browser settings via puppeteer. Can anyone help? Thanks in advance.
My code:
const puppeteer = require('puppeteer');
(async() => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto(process.argv[2], {waitUntil : 'networkidle2' });
await page.screenshot({path: '1.png'});
const client = await page.target().createCDPSession();
var content = await client.send('Network.getAllCookies');
var json = JSON.stringify(content, null, 4);
console.log( json );
var fs = require('fs');
fs.writeFile("output.json", json, 'utf8', function (err) {
if (err) {
console.log("An error occured while writing JSON Object to File.");
return console.log(err);
}
process.exit();
});
})();
Usually the kind of settings that can be set on chrome://settings/cookies can be also set via chrome launch flags (they are listed here), but in case of "Allow all cookies" there seems to be no dedicated flag to do this. Navigating to chrome://settings/cookies and set it with puppeteer is neither an option (pages without a protocol cannot be visited with headless chrome due to a schema validity check, see page.goto
and more detailed explantion by Grant Miller here).
As your question's title already mentions it: you can use a dedicated chrome user profile (let's call it AllowCookies) that already has the required cookie settings. After this profile is created you have multiple ways to use it in puppeteer:
executablePath
and userDataDir
options on puppeteer.launch
;--user-data-dir=
chrome launch flag in args.Note: You can retrieve executable and profile paths on the chrome://version page.
E.g. (with Windows path examples, see more platforms and more info here):
const browser = await puppeteer.launch({
headless: false,
executablePath: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
userDataDir: '%userprofile%\\AppData\\Local\\Google\\Chrome\\User Data\\AllowCookies'
})
or
const browser = await puppeteer.launch({
headless: false,
args: ['--user-data-dir=%userprofile%\\AppData\\Local\\Chromium\\User Data\\AllowCookies']
})
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