【发布时间】:2019-01-30 21:41:37
【问题描述】:
在Github上看到page.setCookie(...cookies)方法有些麻烦。
我想知道是否有人能够使用这种方法保存 cookie 持久性。我已经看到了解决方法(但这不是我的问题)
在这段代码中,我简单地设置了一个 cookie,然后重新加载页面。重新加载页面后,cookie 不再存在。我试图删除过期时间,将其设置为 0、1、-1 或将来的 Unix 时间戳......没有用
const puppeteer = require('puppeteer');
const OPT = {
headless: false,
args: ['--no-sandbox', '--disable-setuid-sandbox', '--user-data-dir']
};
const COOKS =
[ { name: 'tdsess',
value: 'TEST_DRIVE_SESSION',
domain: 'testing-ground.scraping.pro',
path: '/',
expires: -1,
size: 24,
httpOnly: false,
secure: false,
session: true } ];
(async () => {
const URL = `http://testing-ground.scraping.pro/login`
const browser = await puppeteer.launch(OPT)
const page = await browser.newPage()
await page.goto(URL, { 'waitUntil' : 'networkidle2' })
await page.setCookie(...COOKS)
let cook = await page.cookies()
console.log(`==== first login ====`)
console.log(cook)
console.log(`==================`)
await page.reload()
console.log(`after reload`)
setInterval(async () => {
cook = await page.cookies()
console.log(cook)
}, 3000)
})()
这将输出:
==== first login ====
[ { name: 'tdsess',
value: 'TEST_DRIVE_SESSION',
domain: 'testing-ground.scraping.pro',
path: '/',
expires: -1,
size: 24,
httpOnly: false,
secure: false,
session: true } ]
==================
after reload
[]
[]
我使用的版本:
* Puppeteer 1.7.0 * Nodejs 10.8.0 *
【问题讨论】:
标签: javascript node.js cookies puppeteer