2

I'm having issues getting puppeteer to use the default profile that my chrome browser uses. I've tried setting path to the user profile, but when I go to a site with puppeteer that I know is saved with chrome app's userDataDir, there's nothing saved there. What am I doing wrong? I appreciate any help!

   const browser = await puppeteer.launch({
            headless: false,
            userDataDir: 'C:\\Users\\Bob\\AppData\\Local\\Google\\Chrome\\User Data',
        }).then(async browser => {

I've also tried userDataDir: 'C:/Users/Phil/AppData/Local/Google/Chrome/User Data',, but still nothing.

UPDATED:

const username = os.userInfo().username;

(async () => {
    try {
        const browser = await puppeteer.launch({
            headless: false, args: [
                `--user-data-dir=C:/Users/${username}/AppData/Local/Google/Chrome/User Data`]
        }).then(async browser => {
user6680
  • 79
  • 6
  • 34
  • 78
  • Have you tried looking into this https://stackoverflow.com/questions/53236692/how-to-use-chrome-profile-in-puppeteer ? – Adriano Mar 17 '21 at 00:12
  • I tried doing it like in the answer from that post, but it's still not auto populating the fields. Do you know why that is? See ```UPDATED:``` section to see how I have it setup now. – user6680 Mar 18 '21 at 22:18
  • @user6680 feel free to add the answer to your question & mark as solved rather than including the answer in your question. It will more clearly help the next person. – serakfalcon Mar 23 '21 at 03:24

1 Answers1

0

I had same exact issue before. However connecting my script to a real chrome instance helped to solve a lot of problems specially the profile one.

You can see the steps here: https://medium.com/@jaredpotter1/connecting-puppeteer-to-existing-chrome-window-8a10828149e0

//MACOS
/*
Open this instance first:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222 --no-first-run --no-default-browser-check --user-data-dir=$(mktemp -d -t 'chrome-remote_data_dir')
// Windows:
- Add this to Target of launching chrome --remote-debugging-port=9222
- Navigate to http://127.0.0.1:9222/json/version
- copy webSocketDebuggerUrl
More Info: https://medium.com/@jaredpotter1/connecting-puppeteer-to-existing-chrome-window-8a10828149e0
*/
// Puppeteer Part
// Always update this socket after running the instance in terminal (look up ^)

and this is abstracted controller written in Typescript, that I always use in any project:

import * as puppeteer from 'puppeteer';
import { Browser } from 'puppeteer/lib/cjs/puppeteer/common/Browser';
import { Page } from 'puppeteer/lib/cjs/puppeteer/common/Page';
import { PuppeteerNode } from 'puppeteer/lib/cjs/puppeteer/node/Puppeteer';
import { getPuppeteerWSUrl } from './config/config';

export default class Puppeteer {
  public browser: Browser;
  public page: Page;

  getBrowser = () => {
    return this.browser;
  };

  getPage = () => {
    return this.page;
  };

  init = async () => {
    const webSocketUrl = await getPuppeteerWSUrl();
    try {
      this.browser = await ((puppeteer as unknown) as PuppeteerNode).connect({
        browserWSEndpoint: webSocketUrl,
        defaultViewport: {
          width: 1920,
          height: 1080,
        },
      });
      console.log('BROWSER CONNECTED OK');
    } catch (e) {
      console.error('BROWSER CONNECTION FAILED', e);
    }
    this.page = await this.browser.newPage();
    this.page.on('console', (log: any) => console.log(log._text));
  };
}

Abstracted webosocket fecther:

import axios from "axios";
import { exit } from "process";

export const getPuppeteerWSUrl = async () => {
  try {
    const response = await axios.get("http://127.0.0.1:9222/json/version");

    return response.data.webSocketDebuggerUrl;
  } catch (error) {
    console.error("Can not get puppeteer ws url. error %j", error);
    console.info(
      "Make sure you run this command (/Applications/Google Chrome.app/Contents/MacOS/Google Chrome --remote-debugging-port=9222 --no-first-run --no-default-browser-check --user-data-dir=$(mktemp -d -t 'chrome-remote_data_dir')) first on a different shell"
    );
    exit(1);
  }
};

Feel free to adjust the template to suit whatever you enviroment/tools currrently look like.

ProllyGeek
  • 15,517
  • 9
  • 53
  • 72