Python Selenium中ChromeDriver无法运行,求助排查Mac安装脚本问题
Let’s break down the most likely reasons your ChromeDriver isn’t playing nice with Selenium on your new Mac, and how to fix them step by step:
1. Chrome Browser ↔ ChromeDriver Version Mismatch
This is the #1 culprit more often than not. ChromeDriver must match the exact major version of your Chrome browser. Here’s how to verify:
- Open Chrome, go to
Settings > About Chrometo get your browser version (e.g., 120.0.6099.109) - Run this in terminal to check your ChromeDriver version:
chromedriver --version
If the versions don’t align:
- Either update/downgrade Chrome to match your ChromeDriver, or
- Manually download the matching ChromeDriver version and replace the one installed via Homebrew (usually located at
/usr/local/bin/chromedriver)
2. Broken or Temporary PATH Configuration
Your install script uses export PATH=$PATH:/..., but that only applies to the current terminal session. Plus, you might have entered the wrong path entirely.
- First, find ChromeDriver’s actual install location with:
which chromedriver - To make the path permanent (new macOS uses zsh by default), add this to your
~/.zshrcfile:echo 'export PATH="$PATH:/usr/local/bin"' >> ~/.zshrc source ~/.zshrc - Alternatively, skip PATH setup entirely and point directly to ChromeDriver in your Python script:
from selenium import webdriver # For Selenium versions <4.6.0 driver = webdriver.Chrome(executable_path="/usr/local/bin/chromedriver") # For Selenium 4.6.0+ (auto-manages drivers, but you can still specify if needed) from selenium.webdriver.chrome.service import Service driver = webdriver.Chrome(service=Service("/usr/local/bin/chromedriver"))
3. macOS Security Blocking ChromeDriver
Fresh Macs block unsigned third-party tools by default. If you see a pop-up saying ChromeDriver can’t be opened, or get a terminal error about permissions:
- Go to
System Settings > Privacy & Security > General– you’ll see a notice that ChromeDriver was blocked. Click "Allow" - Or run this terminal command to remove the quarantine flag:
xattr -d com.apple.quarantine /usr/local/bin/chromedriver
4. Outdated Selenium Version
Older Selenium versions might not support newer ChromeDriver releases. Check your Selenium version:
pip show selenium
If it’s below version 4, upgrade it:
pip install --upgrade selenium
5. Test ChromeDriver Alone First
Before blaming your Python script, verify ChromeDriver works on its own. Run this in terminal:
chromedriver
If you see output like Starting ChromeDriver 120.0.6099.109 ... on port 9515, then ChromeDriver is functional – the issue is in your Selenium code. If you get errors here, fix those first (permissions, version, etc.).
Start with checking version mismatch and PATH setup – those are the most common fixes for fresh macOS setups.
内容的提问来源于stack exchange,提问作者Daniel Stojanovic

