使用Python+Selenium初始化带代理的ChromeDriver时遭遇异常
Hey there, let's work through this proxy initialization issue you're facing with Chrome WebDriver. Based on the code snippet and partial error stack you shared, here's a breakdown of likely problems and actionable fixes:
First, Let's Recap Your Setup
Your code attempt:
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='/usr/lib/chromium-browser/chromedriver')
Partial error trace:
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 75, in init
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 154, in __...
Common Causes & Fixes
1. Incorrect Proxy Configuration in ChromeOptions
Older versions of Selenium (especially those compatible with Python 2.7) require specific syntax for proxy settings. If you didn't properly format the proxy argument, this will trigger initialization failures.
Fix: Ensure your proxy is configured with the correct protocol and format:
from selenium import webdriver from selenium.webdriver.chrome.options import Options # Initialize ChromeOptions correctly chrome_options = Options() # Replace with your actual proxy details (include http/https protocol!) chrome_options.add_argument('--proxy-server=http://your_proxy_address:your_proxy_port')
2. ChromeDriver & Chrome Browser Version Mismatch
This is one of the most frequent causes of initialization errors. Your Chromium browser and Chromedriver must be version-compatible (usually within 1-2 major versions).
Fix:
- Check your Chromedriver version:
/usr/lib/chromium-browser/chromedriver --version - Check your Chromium browser version:
chromium-browser --version - If versions don't align, download the matching Chromedriver for your browser version.
3. Incorrect Executable Path or Permissions
If the Chromedriver path is wrong, or the file lacks execution permissions, the driver won't launch.
Fix:
- Verify the path exists:
ls -l /usr/lib/chromium-browser/chromedriver - Add execution permissions if missing:
sudo chmod +x /usr/lib/chromium-browser/chromedriver
4. Deprecated chrome_options Parameter
In some older Selenium versions, the chrome_options parameter was renamed to options. Try swapping the parameter name:
Fix:
driver = webdriver.Chrome(options=chrome_options, executable_path='/usr/lib/chromium-browser/chromedriver')
5. Proxy Authentication (Tricky for Old Versions)
If your proxy requires a username and password, older ChromeDriver versions can't handle this via add_argument. You'll need to create a simple Chrome extension to inject auth credentials.
Quick Fix for Auth:
- Create a file named
proxy_auth.zipcontaining two files:manifest.json:{ "version": "1.0.0", "manifest_version": 2, "name": "Chrome Proxy Auth", "permissions": ["proxy", "tabs", "unlimitedStorage", "storage", "<all_urls>"], "background": { "scripts": ["background.js"] } }background.js:chrome.proxy.settings.set({value: {mode: "fixed_servers", rules: {singleProxy: {scheme: "http", host: "YOUR_PROXY_IP", port: YOUR_PROXY_PORT}}}, scope: "regular"}, function() {}); chrome.webRequest.onAuthRequired.addListener( function(details) { return { authCredentials: {username: "YOUR_USERNAME", password: "YOUR_PASSWORD"} }; }, {urls: ["<all_urls>"]}, ["blocking"] );
- Load the extension in your ChromeOptions:
chrome_options.add_extension('proxy_auth.zip')
Troubleshooting Step to Isolate the Issue
First, try initializing the driver without proxy settings to confirm the core driver works:
driver = webdriver.Chrome(executable_path='/usr/lib/chromium-browser/chromedriver') driver.get("https://google.com")
If this works, the problem is definitely tied to your proxy configuration.
内容的提问来源于stack exchange,提问作者Dip Deb




