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

传递userDataDir给Puppeteer无法读取Chrome已保存的登录状态

Puppeteer调用Chrome默认配置文件无法保留登录状态问题

我想使用Puppeteer(v20.8.0)控制和常规Chrome浏览器相同的配置文件/会话,具体来说,打开新的Puppeteer实例时,已登录的网站能保持登录状态。

但传递userDataDir参数后未达到预期效果:Puppeteer启动的Chrome实例能加载常规Chrome的书签、头像甚至相同的标签页,也会显示“Chrome正由自动化测试软件控制”的提示,但所有在常规Chrome中已登录的网站都没有保持登录状态,疑似Cookie、存储等数据未被正常读取或使用。

我的代码示例:

const os = require('os');
const username = os.userInfo().username;
const workingExePath = '/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome';
const workingDataBaseDir = `/Users/${username}/Library/Application\\ Support/Google/Chrome`

const browser = await puppeteer.launch({
  headless: false,
  executablePath: workingExePath, // supply the user-specific path here
  defaultViewport: {
    width: 1280,
    height: 720
  },
  userDataDir: workingDataBaseDir, // supply the user-specific path here

  args: [
    '--mute-audio', // this mutes the entire browser, not just one tab
    '--profile-directory=Default',
  ]
});

const page = await browser.newPage();
await page.goto('https://mail.google.com/mail/u/0/#inbox');

为解决问题,我手动将原userDataDir复制到/Users/${username}/Library/Application\\ Support/Google/Chrome_(末尾加下划线),并在Puppeteer中使用这个新目录,但问题仍未解决。

内容的提问来源于stack exchange,提问作者Ian W

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 11:05:14