我想使用 Puppeteer (v20.8.0) 来控制与我的常规 Chrome 浏览器相同的配置文件/会话。具体来说,我希望我登录的网站在打开新的 Puppeteer 实例时保持登录状态。
将
userDataDir
传递给 Puppeteer 无法按预期工作。
我能够打开 Puppeteer Chrome 实例,并且该实例肯定具有书签和个人资料图片,甚至与非 Puppeteer 生成的 Chrome 具有相同的选项卡。 Puppeteer 生成的 Chrome 实例还显示“Chrome 正在被自动测试软件控制”消息。
但是,在 Puppeteer 生成的 Chrome 上,我没有登录通常在非 Puppeteer 生成的 Chrome 浏览器上访问的任何网站。所以这就像 cookie、存储或其他任何东西无法正常工作或没有被 Puppeteer 或其他东西使用。
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 来使用这个新文件夹,但它仍然不起作用。
一些建议:
./
)配置文件和 cookie 正在工作,而在旧位置,它非常挑剔,有时会抛出“无法启动”浏览器进程”错误,有时无法正确拾取cookie。chrome://version/
来查看最终生效的真实位置,有时它不是我想象的那样。 const browser = await puppeteer.connect({
browserURL: 'http://localhost:9222'
})