In Puppeteer how to switch to chrome window from default profile to desired profile
Okay, I found the reason.
But first i would like to add that the issue is not in puppeteer. The issue is how chromium flag --user-data-dir
works and the way i expected it to work.
My understanding was that arg --user-data-dir
is specified to change default dir for user data. The default dir where chromium searches for user data is %userprofile%\AppData\Local\Chromium\User Data
but when arg --user-data-dir
is used then it appends '\Default' to specific dir. So it becomes %userprofile%\AppData\Local\Chromium\User Data\Default
instead, which is a profile directory.
Hence there was no need of arg --profile-directory
. But since I had used it, I also instructed chromium to consider using specific profile.
There is definitely some clash of args here which led to opening of 2 browsers. One with specified profile and another with default.
So what I did instead is:
I moved contents of directory
%userprofile%\AppData\Local\Chromium\User Data\Profile 1
to%userprofile%\AppData\Local\Chromium\User Data\Profile 1\Default
. I created 'Default' Directory inside Profile 1 directory.Removed arg
--profile-directory
and set--user-data-dir=%userprofile%\AppData\Local\Chromium\User Data\Profile 1
.
Now, what chromium does is, it changes it to %userprofile%\AppData\Local\Chromium\User Data\Profile 1\Default
. This way I can use puppeteer to launch using desired profile.
Final Code:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({headless:false, args:['--disable-extensions-except=/path/to/my/extension',
'--load-extension=/path/to/my/extension',
'--user-data-dir=%userprofile%\\AppData\\Local\\Chromium\\User Data\\Profile 1'
//'--profile-directory=Profile 1'
]});
const page = await browser.newPage();
await page.goto("http://www.google.com");
await page.waitFor(5000)
await browser.close();
})();
Thanks for reading.