使用Selenium ChromeDriver连接Socks5代理无法访问网站求助
Fixes for SOCKS5 Proxy Authentication in Selenium Chrome
Your code has three critical issues preventing proxy functionality:
1. Incorrect Proxy Scheme
You're using a SOCKS5 proxy but configured the extension to use http as the scheme. This breaks proxy routing entirely. Update the scheme value in background_js to socks5:
singleProxy: { scheme: "socks5", // Changed from "http" host: "{PROXY_HOST}", port: parseInt({PROXY_PORT}) },
2. Invalid ChromeDriver Path Setup
The --chromedriver-executable flag is not a valid Chrome argument. Use Selenium's Service class to specify the driver path (required for Selenium 4+):
from selenium.webdriver.chrome.service import Service # Inside get_chromedriver() chromedriver_path = 'C:/Users/User/AppData/Local/Programs/Python/Python311/Scripts/chromedriver-win64/chromedriver.exe' service = Service(chromedriver_path) driver = webdriver.Chrome(service=service, options=chrome_options)
3. Manifest V2 Compatibility (Chrome >=115)
Modern Chrome versions block Manifest V2 extensions by default. Add these flags to re-enable support:
chrome_options.add_argument("--enable-features=LegacyExtensionManifestV2") chrome_options.add_argument("--disable-features=ExtensionManifestV2Blocklist")
Optional: Migrate to Manifest V3
For long-term compatibility, switch to Manifest V3 (Chrome's current standard). Replace your manifest_json and background_js with:
Manifest V3 Code
manifest_json = f''' {{ "version": "1.0.0", "manifest_version": 3, "name": "Chrome Proxy", "permissions": ["proxy", "storage", "webRequest"], "host_permissions": ["<all_urls>"], "background": {{"service_worker": "background.js"}}, "minimum_chrome_version": "109.0.0" }} ''' background_js = f''' var config = {{ mode: "fixed_servers", rules: {{ singleProxy: {{ scheme: "socks5", host: "{PROXY_HOST}", port: parseInt({PROXY_PORT}) }}, bypassList: ["localhost"] }} }}; chrome.proxy.settings.set({{value: config, scope: "regular"}}, () => {{}}); chrome.webRequest.onAuthRequired.addListener( details => ({{ authCredentials: {{ username: "{PROXY_USER}", password: "{PROXY_PASS}" }} }}), {{urls: ["<all_urls>"]}}, ['blocking'] ); '''
Additional Troubleshooting Steps
- Test Proxy Independently: Use
curlto verify your proxy works:curl --socks5 {PROXY_HOST}:{PROXY_PORT} --proxy-user {PROXY_USER}:{PROXY_PASS} https://httpbin.org/ip - Version Compatibility: Ensure Chrome and ChromeDriver share the same major version.
- Refresh Extension: Delete the old
proxy_extension.zipbefore running the script to avoid using cached files.
Full Corrected Code (Manifest V2)
import os import zipfile from selenium import webdriver from selenium.webdriver.chrome.service import Service PROXY_HOST = '31.43.185.85' PROXY_PORT = 57425 PROXY_USER = '443808b7f7b8' PROXY_PASS = '6526ade8d11a' manifest_json = f''' {{ "version": "1.0.0", "manifest_version": 2, "name": "Chrome Proxy", "permissions": [ "proxy", "tabs", "unlimitedStorage", "storage", "<all_urls>", "webRequest", "webRequestBlocking" ], "background": {{"scripts": ["background.js"]}}, "minimum_chrome_version": "22.0.0" }} ''' background_js = f''' var config = {{ mode: "fixed_servers", rules: {{ singleProxy: {{ scheme: "socks5", host: "{PROXY_HOST}", port: parseInt({PROXY_PORT}) }}, bypassList: ["localhost"] }} }}; chrome.proxy.settings.set({{value: config, scope: "regular"}}, function() {{}}); function callbackFn(details) {{ return {{ authCredentials: {{ username: "{PROXY_USER}", password: "{PROXY_PASS}" }} }}; }} chrome.webRequest.onAuthRequired.addListener( callbackFn, {{urls: ["<all_urls>"]}}, ['blocking'] ); ''' def create_proxy_extension(): with zipfile.ZipFile('proxy_extension.zip', 'w') as zp: zp.writestr("manifest.json", manifest_json) zp.writestr("background.js", background_js) def get_chromedriver(use_proxy=False, user_agent=None): path = os.path.dirname(os.path.abspath(__file__)) if use_proxy: create_proxy_extension() chrome_options = webdriver.ChromeOptions() if use_proxy: chrome_options.add_extension(os.path.join(path, 'proxy_extension.zip')) if user_agent: chrome_options.add_argument(f'--user-agent={user_agent}') # Enable Manifest V2 support for modern Chrome chrome_options.add_argument("--enable-features=LegacyExtensionManifestV2") chrome_options.add_argument("--disable-features=ExtensionManifestV2Blocklist") # Configure ChromeDriver chromedriver_path = 'C:/Users/User/AppData/Local/Programs/Python/Python311/Scripts/chromedriver-win64/chromedriver.exe' service = Service(chromedriver_path) driver = webdriver.Chrome(service=service, options=chrome_options) return driver def main(): driver = get_chromedriver(use_proxy=True) driver.get('https://httpbin.org/ip') if __name__ == '__main__': main()
内容的提问来源于stack exchange,提问作者Marianne Sanders
相关产品推荐
相关产品推荐

