You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Puppeteer持久化登录Cookie失效,求问题排查

问题分析与修复方案

核心问题点

  • 登录时依赖固定延时保存Cookie,时机不可靠,可能导致Cookie未完全写入就被保存
  • 恢复Cookie前未先导航到目标域名页面,Puppeteer无法正确设置带域名、HttpOnly等属性的Cookie
  • 未处理Cookie序列化后可能出现的字段格式问题

具体修复步骤

1. 优化登录时的Cookie保存逻辑

替换固定sleep为等待登录成功后的页面标识元素,确保Cookie已完全生成;同时只保存路透社域名下的Cookie,避免无关Cookie干扰。

修改后的登录代码:

const puppeteer = require('puppeteer');
const fs = require('fs').promises;

(async () => {
  const browser = await puppeteer.launch({
    headless: false,
    executablePath: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
    defaultViewport: null,
  });
  const page = await browser.newPage();
  const link = 'https://www.reuters.com/account/sign-in/?redirect=https%3A%2F%2Fwww.reuters.com%2F';

  await page.goto(link, { waitUntil: "networkidle2" });

  await page.waitForSelector('input[name="email"]', { visible: true });
  await page.waitForSelector('input[name="password"]', { visible: true });

  await page.type('input[name="email"]', "email@email.com");
  await page.type('input[name="password"]', "myComplicatedPassword");

  await page.click('.sign-in-form__sign-in-btn__2jvFh');

  // 等待登录成功后的用户菜单元素,确保页面完全加载、Cookie已写入
  await page.waitForSelector('.site-header__user-menu__3X0Xz', { visible: true, timeout: 15000 });

  // 仅保存reuters.com域名下的Cookie
  const cookies = await page.cookies('https://www.reuters.com');
  await fs.writeFile('./cookies.json', JSON.stringify(cookies, null, 2));

  await browser.close();
})();

2. 修复Cookie恢复逻辑

恢复Cookie前必须先导航到路透社页面,确保页面处于目标域名环境;同时处理Cookie字段的格式问题,比如将序列化后的字符串类型expires转为数字。

修改后的恢复代码:

const puppeteer = require('puppeteer');
const fs = require('fs').promises;

(async () => {
  const browser = await puppeteer.launch({
    headless: false,
    executablePath: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
    defaultViewport: null,
  });
  const page = await browser.newPage();

  // 先导航到路透社主页,确保域名匹配,为设置Cookie提供正确环境
  await page.goto('https://www.reuters.com/', { waitUntil: 'networkidle2' });

  // 读取并处理Cookie
  const cookiesString = await fs.readFile("./cookies.json", 'utf-8');
  let cookies = JSON.parse(cookiesString);

  // 修复expires字段类型:JSON序列化后可能转为字符串,需转回数字
  cookies = cookies.map(cookie => {
    if (cookie.expires && typeof cookie.expires === 'string') {
      cookie.expires = parseInt(cookie.expires, 10);
    }
    return cookie;
  });

  // 设置Cookie
  await page.setCookie(...cookies);

  // 重新加载页面验证登录状态
  await page.reload({ waitUntil: 'networkidle2' });

  // 检查是否登录成功(示例:判断用户菜单元素是否存在)
  const isLoggedIn = await page.$('.site-header__user-menu__3X0Xz') !== null;
  console.log('登录状态:', isLoggedIn ? '已登录' : '未登录');

  // await browser.close();
})();

额外注意事项

  • 使用fs.promises版本的文件操作,避免异步回调问题
  • 部分网站会定期刷新Cookie或使用Session Cookie,需定期重新登录更新Cookie
  • 反爬严格的网站可能对无头模式有检测,建议保持headless: false
  • 确保页面处于HTTPS环境(路透社本身是HTTPS),否则无法设置secure属性的Cookie

内容的提问来源于stack exchange,提问作者Hicham Al Walli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 20:09:24