Jenkins上Selenium测试报WebDriverError: unknown session问题求助
Let’s break down your issue step by step—this "unknown session" error almost always means the connection between your test code and the Chrome browser instance dropped unexpectedly, which explains why it works locally but fails on Jenkins. Here are targeted fixes based on your stack and symptoms:
1. Double-Check Chrome-Chromedriver Compatibility (Don’t Assume It’s Correct)
While Chrome 65.0.3325.146 should pair with Chromedriver 2.36.540471, Jenkins servers sometimes auto-update browsers in the background without you noticing.
- SSH into your Jenkins agent/runner and run these commands to confirm versions:
google-chrome --version chromedriver --version
If versions don’t match, download the exact Chrome 65 build and matching Chromedriver, then replace the versions on your Jenkins node to restore compatibility.
2. Fix Headless Mode Quirks (The #1 Culprit for Jenkins Issues)
Most Jenkins Selenium runs use headless Chrome, and older headless versions (like Chrome 65) have known bugs with PWAs, modals, and resource limits. Add these critical arguments to your ChromeOptions to stabilize the session:
ChromeOptions options = new ChromeOptions(); options.addArguments("--headless"); options.addArguments("--disable-gpu"); // Required for older headless Chrome builds options.addArguments("--no-sandbox"); // Bypasses OS security restrictions common in Jenkins environments options.addArguments("--disable-dev-shm-usage"); // Fixes shared memory limits in VMs/containers options.addArguments("--incognito"); // Ensures clean PWA cache between test runs WebDriver driver = new ChromeDriver(options);
If possible, temporarily disable headless mode (use Xvfb to simulate a display on Jenkins) to see if the modal loads normally—this will confirm if headless mode is causing the hang.
3. Stabilize Your Selenium Server
Selenium Standalone 3.11.0 is quite outdated, and it has known session management bugs that can cause drops under load.
- Restart the Selenium Server: A quick restart can clear stuck or corrupted sessions.
- Upgrade to Selenium 3.141.59: This is the last stable release of Selenium 3, which fixes many session-related bugs without breaking compatibility with your Chrome/Chromedriver versions.
- Check Grid Timeouts: If using Selenium Grid, ensure your node doesn’t have overly aggressive
sessionTimeoutsettings—increase it temporarily to rule out timeout-induced session loss.
4. Fix Resource Limits on Jenkins
Jenkins nodes often have tight memory/CPU limits, and PWAs + modals can push Chrome over the edge, causing it to crash (and lose the session).
- Monitor Node Resources: Check the Jenkins node’s CPU/memory usage during the test. If it spikes to 100%, allocate more resources to the node.
- Replace Hard Sleeps with Explicit Waits: Your test might be trying to interact with the modal before it’s fully loaded, leading to hangs and eventual session loss. Use explicit waits instead:
WebDriverWait wait = new WebDriverWait(driver, 10); WebElement modal = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("your-modal-id")));
5. Clear PWA Cache & Service Worker Data
PWAs rely heavily on caching, and Jenkins’ browser environment might have stale cache causing the modal to fail. The --incognito argument helps, but you can add these to ensure a clean state:
options.addArguments("--disable-cache"); options.addArguments("--disable-service-worker");
内容的提问来源于stack exchange,提问作者tiglatpilesar

