如何解决谷歌驱动出现的org.openqa.selenium.WebDriverException连接本地主机失败问题
Hey Raj, sorry to hear you’ve been wrestling with this frustrating connection issue for a week—let’s walk through some targeted fixes to get your Selenium setup back on track.
First, let’s recap the core error you’re hitting:
java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:4321
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:33:15.31Z'
Here are the most likely culprits and how to fix them:
1. Check if Port 4321 is Being Occupied
The error points to a failure connecting to port 4321, which means another process might be hogging that port, or ChromeDriver isn’t binding to it correctly.
- On Windows, run this command in Command Prompt to check port usage:
netstat -ano | findstr :4321 - Look for the PID (process ID) in the output, then open Task Manager, find that PID, and end the process if it’s an unrelated app.
- Alternatively, tell Selenium to use a different port for ChromeDriver in your code. For example:
ChromeDriverService service = new ChromeDriverService.Builder() .usingPort(4444) // Pick an unused port .build(); WebDriver driver = new ChromeDriver(service);
2. Verify Chrome <-> ChromeDriver Compatibility
Even though you upgraded/downgraded ChromeDriver, make sure it’s fully compatible with your installed Chrome version:
- ChromeDriver 2.36 supports Chrome 63-65, while 2.37 supports 64-66.
- Check your Chrome version by going to
chrome://settings/help, then download the exact matching ChromeDriver version (match the version ranges closely). - If your Chrome auto-updated recently, you might need to upgrade ChromeDriver to match the new Chrome version, not downgrade.
3. Test ChromeDriver Manually to Catch Startup Errors
Sometimes ChromeDriver fails to start silently, causing the connection error. Let’s test it directly:
- Navigate to your ChromeDriver executable’s folder in Command Prompt.
- Run:
chromedriver.exe --port=4321 - Look for any error messages in the console (like "Chrome not found" or permission issues). If you see an error about Chrome’s path, specify it explicitly in your code:
ChromeOptions options = new ChromeOptions(); options.setBinary("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"); // Adjust path as needed WebDriver driver = new ChromeDriver(options);
4. Rule Out Firewall/Antivirus Interference
Your system’s firewall or antivirus might be blocking ChromeDriver from listening on port 4321.
- Temporarily disable your firewall/antivirus (only for testing, make sure you’re on a secure network) and see if the error goes away.
- If it works, add ChromeDriver and your Java process to the allowed list in your security software.
5. Check for Localhost Resolution Issues
Rarely, localhost might not resolve correctly on your system. Try replacing localhost with 127.0.0.1 in your Selenium configuration if applicable, or verify your hosts file (located at C:\Windows\System32\drivers\etc\hosts on Windows) has this line uncommented:127.0.0.1 localhost
Start with these steps—port conflicts and compatibility mismatches are the most common causes here. Let me know if any of these fix the issue, or if you see new error messages when testing!
内容的提问来源于stack exchange,提问作者Raj

