Jenkins上ChromeDriver无法启动Chrome,本地正常求技术支持
Hey there! I totally get how frustrating this issue can be when you're just starting out with Selenium and Jenkins—wasting time on fixes that don't work is the worst. Let's walk through the most reliable solutions for this specific error:
Common Causes & Fixes
1. Run Chrome in Headless Mode (Non-Negotiable for Jenkins)
Jenkins runs in a headless environment (no graphical display), so Chrome needs explicit flags to launch without a window. Add these options when initializing your ChromeDriver:
ChromeOptions options = new ChromeOptions(); // Use the modern headless mode (Chrome 112+) options.addArguments("--headless=new"); // Disable GPU acceleration (safe fallback for older environments) options.addArguments("--disable-gpu"); // Bypass OS security sandbox (critical for Jenkins' restricted permissions) options.addArguments("--no-sandbox"); // Pass these options to your ChromeDriver instance WebDriver driver = new ChromeDriver(options);
This is the most common fix—Jenkins can't render a Chrome window natively, so headless mode is a must.
2. Specify a Writable User Data Directory
Chrome often fails to start in Jenkins because it can't access its default profile directory (due to permissions). Define a dedicated, writable temp directory:
// Use a temp directory Jenkins can write to options.addArguments("--user-data-dir=/tmp/jenkins-chrome-profile"); // Optional: Disable shared memory usage to avoid conflicts options.addArguments("--disable-dev-shm-usage");
The --disable-dev-shm-usage flag fixes issues where Chrome runs out of shared memory in containerized or restricted Jenkins environments.
3. Verify Chrome & ChromeDriver Version Match
Even if it works locally, a version mismatch between Chrome on your Jenkins agent and ChromeDriver will cause crashes.
- On your Jenkins agent, run
google-chrome --versionto get Chrome's exact version - Ensure your ChromeDriver version matches this major version (e.g., Chrome 120 needs ChromeDriver 120.x.x.x)
4. Use a Virtual Display (Linux Jenkins Agents)
If your Jenkins agent is on Linux, you might need to simulate a graphical display with Xvfb:
- Install Xvfb on the agent:
sudo apt-get install xvfb(for Debian/Ubuntu) - Update your Jenkins build step to wrap your test command:
xvfb-run mvn test
This creates a virtual display that Chrome can use, even though Jenkins doesn't have a physical screen.
5. Check Jenkins Job Permissions
Make sure the user running your Jenkins job has write access to the directories Chrome uses (like the temp profile dir you set). Add a quick build step to verify:
ls -ld /tmp/jenkins-chrome-profile
If permissions are restricted, adjust them or choose a different directory that the Jenkins user can access.
Integrate These Fixes Into Your Code
Update your initWebsiteDriver method to include the ChromeOptions. For example:
public WebDriver initWebsiteDriver(WebDriver cDriver, String WEB_URL, String Browser) throws NullPointerException { if ("Chrome".equalsIgnoreCase(Browser)) { ChromeOptions options = new ChromeOptions(); options.addArguments("--headless=new"); options.addArguments("--no-sandbox"); options.addArguments("--user-data-dir=/tmp/jenkins-chrome-profile"); options.addArguments("--disable-dev-shm-usage"); cDriver = new ChromeDriver(options); } cDriver.get(WEB_URL); return cDriver; }
Give these steps a try—most likely one (or a combination) will resolve the crash. Good luck!
内容的提问来源于stack exchange,提问作者Pawan Pandey

