How to open multiple persistent chrome profiles using playwright

Question:

I have some Chrome profiles in which I have my different accounts logged in. I’m writing a multi-threaded script that will run each profile using launch_persistent_context so that I’m already logged in and script starts working from thereon.

I’m using Playwright, on Windows 10.

Here’s my code snippet which works for only one profile (I’m not sure why, because the profile it opens is Profile 1 in my file-path, however the script works only for the parent directory, and that too for only a single profile)

def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch_persistent_context(
            channel="chrome", user_data_dir=r"C:\Users\Home\AppData\Local\Google\Chrome\User Data", headless=False)

    page = browser.new_page()

I’m facing difficulties in running all the Chrome profiles simultaneously or even sequentially.

I looked at the docs and GitHub issues but I’m not sure what to do.

Asked By: stuckoverflow

||

Answers:

I’m not very specialist but you can’t use same user_data_dir for multiple instace of context. You should use differnt user_data_dir like

F:/Chrome/Dir1

F:/Chrome/Dir2

This is what I did my self in C# for multiple contexts. I hope be useful

List<IBrowserContext> browserContexts = new List<IBrowserContext>();
IBrowserContext context;
        for (int i = 0; i < 4; i++)
        {
             context = await playwright.Chromium.LaunchPersistentContextAsync($"F:/Program Files/chrome/{i}", new BrowserTypeLaunchPersistentContextOptions
            {
                Headless = false,
                SlowMo = 0,           
            });
            browserContexts.Add(context);                
        }
 

As you see for each browser context, I created a new directory named i which is 0,1,2,3 in this case.

Answered By: ehsan_kabiri_33
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.