Ruby环境下无法连接chromedriver 127.0.0.1的解决方法
Hey there, let's work through this chromedriver connection issue you're facing. First, let's recap your setup to make sure we're aligned:
- Chrome v65
- Chromedriver versions tested: 2.9 to 2.35
- Watir 6.10.3, selenium-webdriver 3.11.0
- Ruby 2.3
The error you're encountering is:
Selenium::WebDriver::Error::WebDriverError: unable to connect to chromedriver 127.0.0.1:9515
来自C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/common/service.rb:142:in
connect_until_stable' 以及C:/Ruby23/lib/ruby/gems/2.3.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/common/service.rb:72:inblock in start'...
Here are the most likely fixes, ordered by priority:
1. Fix Chromedriver-Chrome Version Mismatch
This is the #1 cause of connection issues. Chrome 65 requires Chromedriver 2.37—your tested versions (2.9 to 2.35) are either too outdated or not the exact match for Chrome 65.
- Download Chromedriver 2.37 (pick the Windows version matching your system's 32/64-bit architecture)
- Replace your current chromedriver executable with this correct version.
2. Verify Chromedriver Path & Permissions
- Ensure the chromedriver executable is in a folder included in your Windows
PATHenvironment variable. If not, explicitly set the driver path in your code:require 'watir' Selenium::WebDriver::Chrome.driver_path = 'C:/path/to/your/chromedriver.exe' browser = Watir::Browser.new :chrome - Avoid placing chromedriver in restricted folders like
Program Files—use a user-accessible directory (e.g.,C:\Ruby23\bin) to bypass permission blocks that prevent the driver from starting.
3. Add Chrome Startup Flags to Bypass Common Restrictions
Windows environments sometimes have sandbox or memory constraints that block chromedriver. Try adding these flags to your Chrome options:
require 'watir' options = Selenium::WebDriver::Chrome::Options.new options.add_argument('--no-sandbox') # Bypasses OS security model options.add_argument('--disable-dev-shm-usage') # Overcomes limited resource issues options.add_argument('--disable-gpu') # Disables GPU acceleration (fixes some Windows setup bugs) browser = Watir::Browser.new :chrome, options: options
4. Check for Port 9515 Conflicts
The error points to port 9515, chromedriver's default port, which might be occupied by another process:
- Open Command Prompt and run:
netstat -ano | findstr :9515 - If a process ID (PID) appears, open Task Manager, locate the PID, and end that process.
- Alternatively, force chromedriver to use a different port in your code:
browser = Watir::Browser.new :chrome, port: 9516
5. Update Selenium-Webdriver (If Compatible)
While your selenium-webdriver 3.11.0 works with Watir 6.10.3, updating to the latest compatible version might fix underlying bugs. Run this command in your terminal:
gem update selenium-webdriver
Start with step 1 first—version mismatch is the most common culprit here. Let me know if any of these steps resolve your problem!
内容的提问来源于stack exchange,提问作者Apzal Bahin

