使用Puppeteer在Chrome与Chrome Canary间同步用户数据遇阻
问题详情(基于MacOS)
- 用户数据复制:已将主Chrome配置文件的用户数据目录
/Users/customstandard/Library/Application Support/Google/Chrome/Profile 1
复制到
/Users/customstandard/Library/Application Support/Google/Chrome Canary/Profile 1
- Puppeteer启动配置:
const puppeteer = require('puppeteer-extra') // 添加 stealth 插件并使用默认配置(所有规避技术) const StealthPlugin = require('puppeteer-extra-plugin-stealth') puppeteer.use(StealthPlugin()) const url = "https://www.urlwithpaywall.com"; const timeout = 5000; puppeteer.launch({ headless: false, executablePath: '/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary', userDataDir: "/Users/customstandard/Library/Application\ Support/Google/Chrome\ Canary/Profile\ 1", }).then(async browser => { // 创建新页面 const page = await browser.newPage(); // 设置页面视图 await page.setViewport( { width: 1200, height: 1200, deviceScaleFactor: 1 } ); // 访问目标网站 await page.goto( url, { waitUntil: "domcontentloaded", timeout: timeout } ); // 等待安全验证完成 await page.waitForTimeout(timeout); // 截取全屏截图 await page.screenshot({ path: "screenshot.jpg", fullPage: true }); await browser.close(); });
- 问题现象:执行脚本时,Chrome Canary会在指定的
Profile 1目录下自动创建Default子目录,导致Puppeteer未使用复制的用户数据,而是默认使用新生成的Default目录。查看chrome://version显示配置文件路径为:
/Users/customstandard/Library/Application Support/Google/Chrome Canary/Profile 1/Default
- 预期结果:Puppeteer应使用指定的Chrome Canary下
Profile 1目录中的用户数据。 - 已尝试方案:重命名
Default文件夹、修改userDataDir路径等,但问题仍存在。 - 版本信息:
- puppeteer: 21.6.1
- puppeteer-extra: 3.3.6
- puppeteer-extra-plugin-stealth: 2.11.2
- Chrome Canary: 122.0.6218.0
- Chrome: 120.0.6099.129
- MacOS: Sanoma 14.2.1 (Build 23C71)
解决方案
核心原因
Chrome的userDataDir参数指向的是用户数据根目录,而非单个配置文件目录。当你把Profile 1设为userDataDir时,Chrome会将其识别为根目录,并自动生成Default子目录作为默认配置文件,不会直接读取Profile 1里的原有内容。
修复步骤
方法1:调整目录结构(直接用默认配置)
把复制到Chrome Canary/Profile 1下的所有内容,直接移动到Chrome Canary/Default目录中。这样Chrome启动时会自动加载Default这个默认配置文件,无需额外参数。
方法2:保留Profile 1目录,添加启动参数
如果要保留Profile 1作为配置文件名,需要修改Puppeteer的启动配置:
- 将
userDataDir指向Chrome Canary的用户数据根目录(不要指向Profile 1) - 添加
--profile-directory=Profile 1参数指定要使用的配置文件
修改后的代码:
puppeteer.launch({ headless: false, executablePath: '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary', userDataDir: "/Users/customstandard/Library/Application Support/Google/Chrome Canary", args: ['--profile-directory=Profile 1'] })
注意:路径中的转义符
\可以去掉,JavaScript字符串中直接写空格即可,无需转义。
验证配置
启动脚本后,打开Chrome Canary的chrome://version页面,检查"配置文件路径"是否为:
/Users/customstandard/Library/Application Support/Google/Chrome Canary/Profile 1
如果路径正确,说明配置生效。
额外注意事项
- 启动脚本前,确保Chrome Canary的所有进程都已关闭,避免残留进程导致目录冲突。
- 复制用户数据时,要确保所有文件(包括隐藏文件)都完整复制,避免配置丢失。
内容的提问来源于stack exchange,提问作者CustomStandard
相关产品推荐
相关产品推荐

