简体   繁体   中英

How to open multiple persistent chrome profiles using playwright

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.

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);
            await Task.Delay(500);
        }
 

As you see for each browser context, I created a new directory named i which is 0,1,2,3 in this case. Wait a little between your task to avoid some unwanted results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM