You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Mac环境下无法连接chromedriver服务,寻求解决方法

Troubleshooting "Can not connect to the Service /usr/local/bin/chromedriver" on macOS Sierra 10.12.6

Your Setup & Current Issue

Let's recap what you're dealing with first:

  • Environment: macOS Sierra 10.12.6, PyCharm 2019 CE
  • Code Attempt:
    from selenium import webdriver
    driver = webdriver.Chrome('/usr/local/bin/chromedriver')
    
  • Error:

    selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service /usr/local/bin/chromedriver

You've already worked through some standard fixes: modifying /etc/hosts, setting environment variables, using chromedriver_py, and verifying version matching between Chrome and ChromeDriver. Let's dig into some macOS-Sierra-specific and under-the-hood fixes you might not have tried yet.

Additional Troubleshooting Steps to Try

1. Fix ChromeDriver Permissions & Security Restrictions

macOS Sierra often blocks unsigned binaries like ChromeDriver by default. Let's clear the quarantine flag and ensure the file is executable:

  • Open Terminal and run these commands:
    # Remove the macOS quarantine flag
    xattr -d com.apple.quarantine /usr/local/bin/chromedriver
    # Make sure the binary is executable
    chmod +x /usr/local/bin/chromedriver
    

2. Test ChromeDriver Directly

Let's rule out Selenium as the issue by launching ChromeDriver on its own. In Terminal:

/usr/local/bin/chromedriver
  • If you see an error like Cannot find Chrome binary, you need to explicitly point Selenium to your Chrome app path in code:
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    
    chrome_options = webdriver.ChromeOptions()
    # Update this path if your Chrome is installed elsewhere
    chrome_options.binary_location = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
    
    service = Service("/usr/local/bin/chromedriver")
    driver = webdriver.Chrome(service=service, options=chrome_options)
    

3. Switch to Selenium's Modern Service Class

The executable_path parameter you're using is deprecated in newer Selenium versions. Using the Service class handles path and process management more reliably:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service = Service("/usr/local/bin/chromedriver")
driver = webdriver.Chrome(service=service)

4. Double-Check Version Compatibility for macOS Sierra

macOS Sierra 10.12.6 only supports Chrome up to version 103. Make sure:

  • Your Chrome browser is exactly version 103 (check via Chrome > About Google Chrome)
  • Your ChromeDriver is the matching version for Chrome 103 (download from the official ChromeDriver archive if you need to swap it out)

5. Test Outside PyCharm

PyCharm's environment can sometimes override path settings. Try running your script directly in Terminal:

  • Navigate to your script's folder:
    cd /path/to/your/script
    
  • Run it with your Python interpreter:
    python your_script_name.py
    

If this works but PyCharm fails, check PyCharm's interpreter settings to ensure it's using the same Python environment where you installed Selenium and ChromeDriver.


内容的提问来源于stack exchange,提问作者il5amees

火山引擎 最新活动