puppeteer中的页面cookie无法用于保持登录状态
我想在,而无需重新扫描二维码我知道我必须保存cookie并设置以备下次使用扫描二维码并登录到我的帐户后,我在Google chrome开发工具,应用程序选项卡和Cookie部分中看到了cookie,但是page.cookie为空并返回[]我尝试使用示例代码将cookie存储在文件中。在运行代码mycookie.json并创建[]后,运行代码。
怎么办?
const puppeteer = require('puppeteer');
const jsonfile = require('jsonfile');
(async () => {
const browser = await puppeteer.launch({
headless: false,
args: ['--start-maximized']
});
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36');
await page.setViewport({ width: 1280, height: 800 });
await page.goto('/', {waitUntil: 'networkidle0'});
await page.setViewport({
width: 1440,
height: 900
});
await page.waitFor(10000);//wait for scan qr
console.log(await page.cookies());
// Save Session Cookies
var cookiesFilePath = 'mycookie.json';
const cookiesObject = await page.cookies();
// Write cookies to temp file to be used in other profile pages
jsonfile.writeFile(cookiesFilePath, cookiesObject, { spaces: 2 },
function(err) {
if (err) {
console.log('The file could not be written.', err)
}
console.log('Session has been successfully saved')
})
})();
回答如下:您应该使用userDataDir如果您使用它,则不会丢失浏览器数据。即使您得到的cookie数据也无济于事,因为Web WhatsApp也使用local-storage。
https://github/GoogleChrome/puppeteer/blob/v1.20.0/docs/api.md#puppeteerdefaultargsoptions
https://docs.browserless.io/blog/2019/05/03/improving-puppeteer-performance.html#launch-with-a-user-data-dir
https://chromium.googlesource/chromium/src/+/master/docs/user_data_dir.md
puppeteer中的页面cookie无法用于保持登录状态
我想在,而无需重新扫描二维码我知道我必须保存cookie并设置以备下次使用扫描二维码并登录到我的帐户后,我在Google chrome开发工具,应用程序选项卡和Cookie部分中看到了cookie,但是page.cookie为空并返回[]我尝试使用示例代码将cookie存储在文件中。在运行代码mycookie.json并创建[]后,运行代码。
怎么办?
const puppeteer = require('puppeteer');
const jsonfile = require('jsonfile');
(async () => {
const browser = await puppeteer.launch({
headless: false,
args: ['--start-maximized']
});
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36');
await page.setViewport({ width: 1280, height: 800 });
await page.goto('/', {waitUntil: 'networkidle0'});
await page.setViewport({
width: 1440,
height: 900
});
await page.waitFor(10000);//wait for scan qr
console.log(await page.cookies());
// Save Session Cookies
var cookiesFilePath = 'mycookie.json';
const cookiesObject = await page.cookies();
// Write cookies to temp file to be used in other profile pages
jsonfile.writeFile(cookiesFilePath, cookiesObject, { spaces: 2 },
function(err) {
if (err) {
console.log('The file could not be written.', err)
}
console.log('Session has been successfully saved')
})
})();
回答如下:您应该使用userDataDir如果您使用它,则不会丢失浏览器数据。即使您得到的cookie数据也无济于事,因为Web WhatsApp也使用local-storage。
https://github/GoogleChrome/puppeteer/blob/v1.20.0/docs/api.md#puppeteerdefaultargsoptions
https://docs.browserless.io/blog/2019/05/03/improving-puppeteer-performance.html#launch-with-a-user-data-dir
https://chromium.googlesource/chromium/src/+/master/docs/user_data_dir.md