Skip to content

Add ExcludeSwitches enable-automation and encounter session not created: probably user data directory is already in use, please specify a unique value for --user-data-dir argument #328

Description

@travelliu
Add ExcludeSwitches enable-automation and encounter session not created: probably user data directory is already in use, please specify a unique value for --user-data-dir argument

caps.AddChrome(chrome.Capabilities{
// Path: w.chromeDir,
Args: chromeArgs,
ExcludeSwitches: []string{"enable-automation"},
})

[2025-08-01 01:56:52.360076] DEBUG ChromeDriver D:\code\energy\xyqh5\chrome\chromedriver.exe
[2025-08-01 01:56:53.885060] ERROR NewRemote unknown error - 33: session not created: probably user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
(Driver info: chromedriver=137.0.7151.68 (2989ffee9373ea8b8623bd98b3cb350a8e95cadc-refs/branch-heads/7151@{#1873}),platform=Windows NT 10.0.22631 x86_64)

Remove the parameter and it will be normal This works fine in Python.

service, err := selenium.NewChromeDriverService(w.chromeDriverPath, w.port)
	if err != nil {
		w.l.Errorf("NewSeleniumService %s", err)
		return err
	}
	w.service = service
	// 2. 配置 Chrome Capabilities
	caps := selenium.Capabilities{
		"browserName": "chrome",
	}

	chromeArgs := []string{
		"--start-maximized",
		"--disable-blink-features=AutomationControlled",
		"--mute-audio",
		"--window-size=500,1080", // 设置窗口大小
		"--disable-gpu",          // 禁用 GPU 硬件加速 (有时可避免一些问题)
		// "--no-sandbox",           // 在某些 Linux 环境下需要
		fmt.Sprintf("--user-data-dir=%s", profileDir),
	}
	// 如果需要无头模式,请取消注释下一行
	// chromeArgs = append(chromeArgs, "--headless")

	caps.AddChrome(chrome.Capabilities{
		// Path:            w.chromeDir,
		Args:            chromeArgs,
		ExcludeSwitches: []string{"enable-automation"},
	})
	// chromeOptions := map[string]interface{}{
	// 	"args":            chromeArgs,
	// 	"excludeSwitches": []string{"enable-automation"},
	// }
	// caps["goog:chromeOptions"] = chromeOptions
	// 3. 连接到 ChromeDriver 服务,创建 WebDriver 实例
	wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", w.port))
	if err != nil {
		w.l.Errorf("NewRemote %s", err)
		return err
	}
	w.wd = wd
	err = wd.Get("www.google.com")
	if err != nil {
		w.l.Errorf("Get %s", err)
		return err
	}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions