'Node.js Puppeteer setCookie not setting the cookie
I'm trying to set a certain cookie in the browser using Puppeteer, however when I run the code to set the cookie then console.log all the cookies on the page, it doesn't show up.
I'm running the following:
const captchaCookie = {
name: "hc_accessibility",
value: "drR8A+cddQky0hclwHAG6gjwqk04IGvOhfYMP",
domain: ".hcaptcha.com",
path: "/",
expires: new Date("2022-04-08T09:01:39.010Z").getTime(),
};
await page.setCookie(captchaCookie);
await page.waitForTimeout(5000);
console.log("Writing cookies to file.");
const cookies = await page.cookies();
await fs.writeFile('./cookies.json', JSON.stringify(cookies, null, 2));
console.log("Cookie file written.");
However, when I do this, in my cookies.json file this new cookie I set does not show up for some reason... I suspect this is why the accessibility cookie is not working in the browser for me. This is the JSON output:
[
{
"name": "SCSessionId",
"value": "jijt34hj2xijq4gul5i1fdjx",
"domain": ".scottycameron.com",
"path": "/",
"expires": 1680902482.495723,
"size": 35,
"httpOnly": false,
"secure": false,
"session": false,
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
},
{
"name": "isAdmin",
"value": "False",
"domain": "www.scottycameron.com",
"path": "/",
"expires": -1,
"size": 12,
"httpOnly": false,
"secure": false,
"session": true,
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
},
{
"name": ".ASPXAUTH",
"value": "36268A1E1D2364038C3E1DA55B5D8235E41FAE61D1D8020E4066DB4B01711F9C330386672B7BF44D67388B0032C650E8BA348DB2E8F86E44A4ECFBF1520DB6F0217434729950DA8488724DAB924AC8074C6342355FB76F68C9938A29DFD0AB9CDDEDDF4529B3061816D45AE20578C53CB27FB533EE231BECE07C80BD8B84C777",
"domain": "www.scottycameron.com",
"path": "/",
"expires": 1649367682.495947,
"size": 265,
"httpOnly": true,
"secure": false,
"session": false,
"sameSite": "Lax",
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
},
{
"name": "CookieSession",
"value": "436212140.47873.0000",
"domain": "www.scottycameron.com",
"path": "/",
"expires": -1,
"size": 33,
"httpOnly": true,
"secure": true,
"session": true,
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
},
{
"name": "ak_bmsc",
"value": "4AA3CEC4D9E6AE9969987921E3ED16D4~000000000000000000000000000000~YAAQaKUQAoiJTwKAAQAAPxDnBQ/UlLIQjcFXA9N21BPy51wlF11AbL3wTqAl/x1xqAu2YMBnMxsfEC3vvlHphkagr+XDeaw6triB1ZTEhcauZXYfnTzjgCAJggjaAncgVFF/c8HKZRit5AKZey4Te9kwzf4uiSbCh+nWArwfa9qK7SG0K6EQ15cRWaPx474UU75/Tba2EEryU5+jk/WTBYsKgHCmk2lpt603TQX4+ySHL82ePh2y894prqsPaxMqmOx+NRsdF8COac1GZpBPYNrtBs9efPuWMkY37a1fCb23/SAHUZ1G6QFjgEBiOtHMJ1C/e5o3cxLeXTa3kv90O33HnuINDMLkomSt6VIkCKwi3+JbLeC/tm7Yxb8obHfOQLv58twashH5tvVOrG9gxYSoPeVyhI61HsEcnZuQAm0HfIoIkcpCGg99e04kp0nhhVvZi/5uLG3Rjpj2bzDkOhVUVWI244uiT6u3DFNcXatUkjU5gkM=",
"domain": ".scottycameron.com",
"path": "/",
"expires": 1649373668.746263,
"size": 571,
"httpOnly": true,
"secure": false,
"session": false,
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
},
{
"name": "bm_sv",
"value": "235148FCE2F3956304D0C4670431EDA5~tnhmy/7Lr4mr9f7Xi0o1zWkbgCgatyrkx2dfx8Gz/fAF5zECmrnLYv3VS6IafZMOvoQyuDIu08DH93uSLJFYDvLNmap5md2HffL44BfYpPK+Ozd3BWkiWgHkiHWfgIqrB3h6g0PWd0MQ5m2ILerHuusjawfALt3gZjheilv631o=",
"domain": ".scottycameron.com",
"path": "/",
"expires": 1649373668.992258,
"size": 210,
"httpOnly": true,
"secure": false,
"session": false,
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
},
{
"name": "bm_sz",
"value": "3A185B1E9853A46DEA0835A9A5F079CC~YAAQaKUQAoKJTwKAAQAADwjnBQ9NLqkc3gkbn/VA3Du7UCAd8q+4TkxdiIREjKDGp+JGCdXnNcdgBwUKfpqUM89zXWa1D1VrKxUwZWsOZmT4WKMpS4tqCIR+FygKJBVRuUOVoT9GaG4jhTykIIip7WUuzXMx8D4+Tasu4h/W8Nv/CFKZczch0VIgTrezor06Kf9IwSQZVQvGUsEA6xKryMp3qUcjS+rSn6skmsDgWBca5evadhdldwlf9q/3n7+7c4Ee8IJDbpyG3e2EMr/yzuc31boAChW52yK5ZL3Q3dJy8OsipxqNBVvF~3356486~4272438",
"domain": ".scottycameron.com",
"path": "/",
"expires": 1649380868.647984,
"size": 366,
"httpOnly": false,
"secure": false,
"session": false,
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
},
{
"name": "__RequestVerificationToken_L3N0b3Jl0",
"value": "zS_1VWV9_bWnKOti2t7JemlrNrzVvmOB0hPJxHEyOcg2uWOhlQndSMbVyGrvWnz6_-o1j3oa9kKMC-Aqhb9KSdmGOZZZSZoCsOS7x8CQenc1",
"domain": "www.scottycameron.com",
"path": "/",
"expires": -1,
"size": 144,
"httpOnly": true,
"secure": false,
"session": true,
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
},
{
"name": "_WebStorePublishState",
"value": "PRODUCTION",
"domain": "www.scottycameron.com",
"path": "/",
"expires": -1,
"size": 31,
"httpOnly": false,
"secure": false,
"session": true,
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
},
{
"name": "bm_mi",
"value": "FB564BF7F20BF5685871930524714CF4~m2sVLAiMcIpi9+0BfDpJCBZ9EUgje/DT0VGyJNyrzwu7xK7fASsax/gW10hEK0ZfRWVwLtIeZgZMg+XLNcI6y9P/ZzyGYUR3taytO+KwuvnLw6x3277wU0yXLqgg59hc7qqCJcLUwNAnWnmJhqsoz3UbOxRCLTCiIymcQBTA2tnDeSHeZzWBTrKYoKF+rYhyuS+iukmM6NhwrLyIW9quQIVp+1O4HImjG/minoGBUnoCahXXU4JlPMua+8GL2KtjXIaFn/hXPfRPfWm+ID7BdQZp5Dn4tdR1loSosuZ4jYCw6h2rSFnWrWhAdGASuE8K",
"domain": ".scottycameron.com",
"path": "/",
"expires": 1649373668.294547,
"size": 358,
"httpOnly": true,
"secure": false,
"session": false,
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
},
{
"name": "culture",
"value": "en",
"domain": "www.scottycameron.com",
"path": "/",
"expires": -1,
"size": 9,
"httpOnly": false,
"secure": false,
"session": true,
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
},
{
"name": "_abck",
"value": "F37856EFEE58F3E50D8E7F3ED56968CC~-1~YAAQaKUQApKJTwKAAQAAG1bnBQdv7FpWhEr/RfbfIJSXfoLJJ4Vry+OWo2Olej8t8TPcjeLCeO2AspHip/dA/JxJQj1rud09kIyWHOA0tVTKpe+l8RHtJN8/0HGVRj9mNMrDcC8cmhtrMWLM+xuVoC2PTRFbzHLtW4qIdsxFzEPfCJ073JN/ImF2a0zijnsdb1kvP6q+wy9zIyemST5oQVMGA3Ov6qXqmGoDGinnKRTSx9s9W9b2U2Ba8DOBNz7Hu+IZFw8xh+Tn5SCiRDKPAjPgk5lnG45Rr9oE4ZpnIFwDBUBU+Fkt76cWajtPkKUmlOX7Q+ZeEQSPOCjJeOT0dCaTtK83YET0ECJiz7sDzVBnJNqYUmIGdNZ6pAqI0b9NFri3epB74Y7EHgTDdVy2EGM=~-1~-1~-1",
"domain": ".scottycameron.com",
"path": "/",
"expires": 1680902488.628089,
"size": 458,
"httpOnly": false,
"secure": true,
"session": false,
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
},
{
"name": "_WebStoreculture",
"value": "1",
"domain": "www.scottycameron.com",
"path": "/",
"expires": -1,
"size": 17,
"httpOnly": false,
"secure": false,
"session": true,
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
},
{
"name": "persona",
"value": "other",
"domain": "www.scottycameron.com",
"path": "/",
"expires": -1,
"size": 12,
"httpOnly": false,
"secure": false,
"session": true,
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
},
{
"name": "ASP.NET_SessionId",
"value": "jijt34hj2xijq4gul5i1fdjx",
"domain": "www.scottycameron.com",
"path": "/",
"expires": -1,
"size": 41,
"httpOnly": true,
"secure": false,
"session": true,
"sameSite": "Lax",
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
}
]
Would anyone know the reason why this cookie isn't appearing when I try to get the page's cookies?
Solution 1:[1]
It's because page.cookies() returns cookies only for the current page URL (www.scottycameron.com) and what you've set is considered a 3rd party cookie (.hcaptcha.com). If you want to check all cookies (including 3rd party), you can use:
const cookies = await page._client.send('Network.getAllCookies')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | theDavidBarton |
