Puppeteer:Network.setCookie返回无效的字符串参数

时间:2018-03-09 13:13:25

标签: javascript cookies google-chrome-devtools puppeteer google-chrome-headless

我正在学习使用Puppeteer中的CDPSession class,但是我在使用开发工具协议的方法参数时遇到了麻烦。

Dev Tools Network

开发工具文档的Network.setCookie部分显示用于设置name Cookie的参数:name string,但是当我执行此操作时,它会返回错误:Error: Protocol error (Network.setCookie): Invalid parameters value: string value expected

我做错了什么?

const puppeteer = require('puppeteer');

puppeteer.launch().then(async browser => {
    const page = await browser.newPage();
    const client = await page.target().createCDPSession();
    await client.send('Network.enable');
    const setCookie = await client.send('Network.setCookie', {
        name: 'mycookie'
    });

    console.log("Set Cookie: " + setCookie.success);
    await page.goto('https://example.com');
    await browser.close();
});

1 个答案:

答案 0 :(得分:0)

答案很简单。我还需要设置值和url或domain params。

const setCookie = await client.send ( 'Network.setCookie', { 
    name: 'mycookie', value: 'Hello', domain: 'https://example.com' 
});