chromedp / chromedp

A faster, simpler way to drive browsers supporting the Chrome DevTools Protocol.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How do i create contexts have multiple profile account same time

suhgie opened this issue · comments

package main

import (
"context"
"fmt"
"log"
"time"

"github.com/chromedp/cdproto/target"
"github.com/chromedp/chromedp"

)

func main() {

profile1Ctx, _ := chromedp.NewExecAllocator(context.Background(), GetAllocatorOptions("Default")...)
profile2Ctx, _ := chromedp.NewExecAllocator(context.Background(), GetAllocatorOptions("Profile 1")...)
profile1Ctx, _ = chromedp.NewContext(profile1Ctx)
profile2Ctx, _ = chromedp.NewContext(profile2Ctx)

if err := chromedp.Run(profile1Ctx,
	chromedp.ActionFunc(func(ctx context.Context) error {
		info, err := target.GetTargetInfo().Do(ctx)
		fmt.Println("Profile default browserContext is:", info.BrowserContextID)
		return err
	}),
); err != nil {
	log.Fatal(err)
}

if err := chromedp.Run(profile2Ctx,
	chromedp.ActionFunc(func(ctx context.Context) error {
		info, err := target.GetTargetInfo().Do(ctx)
		fmt.Println("Profile 1 browserContext is:", info.BrowserContextID)
		return err
	}),
); err != nil {
	log.Fatal(err)
}

time.Sleep(1 * time.Minute)

}

func GetAllocatorOptions(profile string) []func(*chromedp.ExecAllocator) {

return append(chromedp.DefaultExecAllocatorOptions[:],
	chromedp.Flag("headless", false), // To display the browser
	chromedp.Flag(`enable-automation`, false),
	chromedp.Flag("disable-extensions", false),
	chromedp.UserDataDir(`C:\Users\user\AppData\Local\Google\Chrome\User Data`),
	chromedp.Flag("profile-directory", profile),
	chromedp.UserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"),
)

}

RESULT IS:

Profile default browserContext is: 743D4306C59AF8F06594CC7399F95C72
2024/01/18 18:39:22 chrome failed to start:
Opening in existing browser session.