You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows环境下Selenium连接各类驱动均提示无法连接服务求助

Troubleshooting "Can not connect to the Service" Error for Selenium Drivers on Windows

Wow, you’ve already worked through all the standard troubleshooting steps—matching browser/driver versions, configuring PATH, checking hosts, testing permissions, even switching across browsers—and you’re still stuck with the same connection error. Since this happens across Chrome, Firefox, and Edge, it’s almost certainly a system-level network or permission issue rather than a problem with individual drivers. Let’s dig into some less obvious fixes:

1. Check Windows Firewall or Security Software Blocking

Local Selenium drivers run as small background services, and Windows Firewall (or third-party antivirus) might be blocking their ability to communicate with your Python script. Here’s how to fix it:

  • Open Windows Defender Firewall → click "Allow an app or feature through Windows Defender Firewall"
  • Click "Change settings" (you’ll need admin rights)
  • Look for your Python executable (python.exe), chromedriver.exe, geckodriver.exe, or msedgedriver.exe in the list. Make sure both "Private" and "Public" network boxes are checked.
  • If they’re not in the list, click "Allow another app" → browse to the location of each executable and add them manually.

2. Check for Local Port Conflicts

Selenium drivers use default local ports (Chrome/Edge use 9515, Firefox uses 4444) that might be occupied by other apps. Let’s verify and fix this:

  • Open Command Prompt and run:
    netstat -ano | findstr :9515
    
    (Replace 9515 with 4444 if testing Firefox)
  • If you see results, note the PID number at the end of the line. Open Task Manager, go to the "Details" tab, find that PID, and end the process.
  • Alternatively, force your driver to use a custom port in code. For Chrome:
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    # Use a custom port (e.g., 9516) to avoid conflicts
    service = Service(executable_path="C:\\chromedriver.exe", port=9516)
    with webdriver.Chrome(service=service, options=chrome_options) as driver:
        driver.set_window_size(800, 460)
        driver.execute_script("document.body.style.zoom='150%'")
        # Fix the file URL syntax here (your original code had a string formatting error)
        driver.get(f"file://{url}")
        driver.find_element_by_tag_name('body').screenshot(output)
    
    Quick note: Your original driver.get("file:\"+url) has a syntax bug—use f"file://{url}" or driver.get("file:///" + url) to properly format local file paths.

3. Disable System-Wide Proxy Settings

If your Windows system uses a global proxy, it might interfere with local driver connections:

  • Open Settings → Network & Internet → Proxy
  • Toggle off any manual proxy settings, and ensure "Automatically detect settings" is turned on.
  • You can also force the driver to ignore proxies in code:
    # For Chrome
    chrome_options.add_argument("--no-proxy-server")
    # For Firefox
    firefox_options = webdriver.FirefoxOptions()
    firefox_options.set_preference("network.proxy.type", 0)
    

4. Adjust User Account Control (UAC) Settings

Even running as admin, high UAC levels can block local service communication:

  • Open Control Panel → User Accounts → Change User Account Control settings
  • Slide the bar down to "Never notify" (this is for testing—you can reset it later)
  • Restart your computer and re-run your script.

5. Test with Elevated Command Prompt

Sometimes running your script directly from an elevated (admin) Command Prompt bypasses hidden permission issues:

  • Right-click Command Prompt → "Run as administrator"
  • Navigate to your script’s directory and run python your_script.py

Since this issue affects all browsers, start with firewall/port checks first—those are the most common culprits for cross-driver connection failures.

内容的提问来源于stack exchange,提问作者Zaid Al Shattle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:09:28