You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome无头模式无法加载指定配置文件,Selenium测试失败求助

Fix: Chrome Headless Mode Not Loading Custom Profile with Extensions

Hey there, I’ve run into this exact issue before—Chrome’s headless mode has a frustrating default behavior where it ignores your custom profile and spins up a temporary one instead. Let’s break down how to fix this:

Why Your Current Setup Isn’t Working

All those SSL certificate settings and debug port flags you tried don’t address the core problem: headless mode automatically uses a temporary user data directory unless you explicitly tell it to use your custom profile folder. That’s why your extensions and profile settings work in regular mode but vanish in headless.

Step-by-Step Fix

Here’s what you need to add to your ChromeOptions configuration:

  1. Specify your user data directory
    Use the --user-data-dir argument to point Chrome directly to your existing profile’s parent folder. For example:

    // Replace with the actual path to your Chrome User Data folder
    chromeOptions.addArguments("--user-data-dir=C:\\Users\\YourUsername\\AppData\\Local\\Google\\Chrome\\User Data");
    
    • On Linux/macOS, this path might look like ~/.config/google-chrome or ~/Library/Application Support/Google/Chrome.
  2. Target the specific profile (if needed)
    If you’re using a non-default profile (like "Profile 1"), add the --profile-directory argument:

    chromeOptions.addArguments("--profile-directory=Profile 1");
    
  3. Clean up conflicting flags
    Remove the --remote-debugging-port flag for now—port conflicts are common, and it’s not necessary just to load your profile. You can re-add it later if you need debugging, but make sure the port isn’t in use.

Modified Code Example

Here’s how your updated ChromeOptions should look:

chromeOptions.setExperimentalOption("excludeSwitches", Arrays.asList("disable-sync", "enable-logging"));
chromeOptions.addArguments("--enable-sync");
chromeOptions.addArguments("--disable-logging");
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--disable-dev-shm-usage");
chromeOptions.addArguments("--headless=new"); // Note: Use "--headless=new" for newer Chrome versions (v112+)
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--window-size=1280,800");
chromeOptions.addArguments("--allow-insecure-localhost");
// Add these two critical lines
chromeOptions.addArguments("--user-data-dir=/path/to/your/Chrome/User Data");
chromeOptions.addArguments("--profile-directory=YourProfileName");
// Keep your SSL settings if needed
chromeOptions.setAcceptInsecureCerts(true);
chromeOptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
chromeOptions.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);

Important Notes

  • Close all Chrome instances before running tests: If your custom profile is already in use by a regular Chrome window, headless mode won’t be able to access it and will fall back to a temporary profile.
  • Check permissions: On Linux/macOS, make sure your test process has read/write access to the user data directory. The --no-sandbox flag helps with some permission issues, but double-check folder ownership if you hit errors.
  • Extension compatibility: Some extensions don’t work in headless mode. Try disabling all extensions except the one you need for testing to rule out conflicts. If your extension is essential, check if it has an updated version that supports headless.

内容的提问来源于stack exchange,提问作者Raj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:08:42