Puppeteer cannot set cookie?

go directly to all the code:

const puppeteer = require("puppeteer");

; (async () => {
    const browser = await puppeteer.launch({
        headless: false,        // 
        devtools: true,         //  F12
    });

    const page = await browser.newPage();       // 
    await page.waitFor(500);                    //  500ms

    var cookie = {
        name: "ccc",
        value: "cccccccccccccccccc",
        domain: "baidu.com",
        path: "/",
        expires: 7 * 24 * 60 * 60 * 1000
    };
    await page.setCookie(cookie);       // cookie

    await page.waitFor(500);
    await page.goto("http://baidu.com/");       // 
})();

then you can"t find a lot of cookie, Baidu that you set up in F12 of the browser, and the result is a bunch of rubbish to copy others, step by step, no change at all. Demo is also copied according to the official demo of google, and there is no demo about setting cookie in the github project.

is there any boss who has successfully set up cookie?

Apr.09,2021

https://stackoverflow.com/que.


F12 cannot be found because you are out of cookie .

new Date(7 * 24 * 60 * 60 * 1000)//Thu Jan 08 1970 08:00:00 GMT+0800 ()

in fact, you can set up a local http service and debug your own service under puppeteer access.
first of all, the domain attribute in your code is passed to the setCookie method, and the domain name is preceded by a .
secondly, you need to make sure that the domain of url you visit using puppeteer's goto method is the same as the domain when you set cookie. Note that although you are visiting http://a.com, many websites will actually be 302 to https://www.a.com, do not visit this kind of url that will be 302.

Menu