运行Selenium脚本报错:Chrome无法建立有效会话,多会话启动无测试执行
Hey Vijaya, let's tackle this Chromedriver issue you're facing—those duplicate startup logs and stuck test execution are definitely frustrating. Here are the most likely fixes to get your tests running again:
1. Fix Your Driver Cleanup Logic
This is the most common culprit. If your tests aren't properly terminating Chromedriver processes, leftover instances will pile up and block new sessions.
- Make sure you're calling
driver.quit()(notdriver.close()) after each test.close()only shuts the browser window, whilequit()terminates the entire Chromedriver process. - If you're using a test framework like TestNG or JUnit, add the cleanup to an annotated teardown method:
@AfterMethod public void tearDown() { if (driver != null) { driver.quit(); } }
2. Manually Kill Leftover Processes
Before running new tests, clear all existing Chrome and Chromedriver processes from your system:
- Windows: Open Task Manager, find all
chrome.exeandchromedriver.exeentries, and end them. - macOS/Linux: Run these terminal commands:
pkill -f chrome pkill -f chromedriver
3. Explicitly Configure ChromeDriver Startup
Add these options to avoid port conflicts and environment-specific issues:
ChromeOptions options = new ChromeOptions(); // Fixes sandbox-related issues in some environments options.addArguments("--no-sandbox"); // Avoids shared memory limits that can crash Chrome options.addArguments("--disable-dev-shm-usage"); // Explicitly set a fixed port to prevent auto-assignment conflicts ChromeDriverService service = new ChromeDriverService.Builder() .usingPort(9515) // Pick an unused port on your system .build(); WebDriver driver = new ChromeDriver(service, options);
4. Double-Check Version Matching
Even if you've swapped drivers, ensure your Chrome browser and Chromedriver versions are exactly matched:
- Check Chrome's exact version via
Settings > About Chrome - Download the corresponding Chromedriver version (no minor version mismatches!) and replace your current driver.
5. Verify Parallel Test Configuration (If Applicable)
If you're running tests in parallel:
- Ensure each test thread gets its own isolated WebDriver instance (no shared drivers!)
- For TestNG, confirm your
parallelsetting is set tomethodsortestsin your suite XML, so each test gets a fresh driver.
Your Error Logs for Reference
Starting ChromeDriver 87.0.4280.88 (89e2380a3e36c3464b5dd1302349b1382549290d-refs/branch-heads/4280@{#1761}) on port 18242
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
[1607528549.423][WARNING]: FromSockAddr failed on netmask
ChromeDriver was started successfully.
Dec 09, 2020 7:42:31 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Starting ChromeDriver 87.0.4280.88 (89e2380a3e36c3464b5dd1302349b1382549290d-refs/branch-heads/4280@{#1761}) on port 1070
Only local connections are allowed.
内容的提问来源于stack exchange,提问作者Vijaya Sri

