将 userDataDir 传递给 Puppeteer 无法像常规 Chrome 那样访问正确的已保存登录信息

问题描述 投票:0回答:1

我想使用 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 来使用这个新文件夹,但它仍然不起作用。

node.js google-chrome browser automation puppeteer
1个回答
0
投票

一些建议:

  • 对我来说,当我将配置文件位置指向运行它的本地文件夹时,此代码效果更好(
    ./
    )配置文件和 cookie 正在工作,而在旧位置,它非常挑剔,有时会抛出“无法启动”浏览器进程”错误,有时无法正确拾取cookie。
  • 您可以通过在浏览器栏中输入
    chrome://version/
    来查看最终生效的真实位置,有时它不是我想象的那样。
  • 即使您的个人资料运行良好并保留了 cookie,Gmail 本身也可能有额外的木偶检测措施。可能需要 Chromium 或其他浏览器仔细调整才能看起来真实。
  • 您可以尝试的另一件事(取决于您所需的设置)是连接到已配置配置文件并运行的实时浏览器实例,如下所示:
    const browser = await puppeteer.connect({
      browserURL: 'http://localhost:9222'
    })
© www.soinside.com 2019 - 2024. All rights reserved.