M1 Mac上Selenium 4.1.0配置代理失败求助
M1 MacBook Pro上Selenium 4.1.0配置代理失败,无法获取Chromedriver的问题
在M1 MacBook Pro上使用selenium==4.1.0配置代理时,出现无法连接代理、无法获取对应版本chromedriver的错误。已确认代理地址127.0.0.1:7890在Chrome浏览器中可正常使用,报错信息及代理配置代码如下:
报错日志
====== WebDriver manager ====== Current google-chrome version is 111.0.5563 Get LATEST chromedriver version for 111.0.5563 google-chrome Traceback (most recent call last): File "/Users/xiaoqiangjiang/source/reddwarf/backend/pydolphin/venv/lib/python3.10/site-packages/urllib3/connectionpool.py", line 696, in urlopen self._prepare_proxy(conn) File "/Users/xiaoqiangjiang/source/reddwarf/backend/pydolphin/venv/lib/python3.10/site-packages/urllib3/connectionpool.py", line 964, in _prepare_proxy conn.connect() File "/Users/xiaoqiangjiang/source/reddwarf/backend/pydolphin/venv/lib/python3.10/site-packages/urllib3/connection.py", line 371, in connect self._tunnel() File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 920, in _tunnel (version, code, message) = response._read_status() File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/http/client.py", line 287, in _read_status raise RemoteDisconnected("Remote end closed connection without" http.client.RemoteDisconnected: Remote end closed connection without response During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/xiaoqiangjiang/source/reddwarf/backend/pydolphin/venv/lib/python3.10/site-packages/requests/adapters.py", line 439, in send resp = conn.urlopen( File "/Users/xiaoqiangjiang/source/reddwarf/backend/pydolphin/venv/lib/python3.10/site-packages/urllib3/connectionpool.py", line 755, in urlopen retries = retries.increment( File "/Users/xiaoqiangjiang/source/reddwarf/backend/pydolphin/venv/lib/python3.10/site-packages/urllib3/util/retry.py", line 574, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='chromedriver.storage.googleapis.com', port=443): Max retries exceeded with url: /LATEST_RELEASE_111.0.5563 (Caused by ProxyError('Cannot connect to proxy.', RemoteDisconnected('Remote end closed connection without response'))) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/xiaoqiangjiang/source/reddwarf/backend/pydolphin/venv/lib/python3.10/site-packages/requests/api.py", line 61, in request return session.request(method=method, url=url, **kwargs) File "/Users/xiaoqiangjiang/source/reddwarf/backend/pydolphin/venv/lib/python3.10/site-packages/requests/sessions.py", line 542, in request resp = self.send(prep, **send_kwargs) File "/Users/xiaoqiangjiang/source/reddwarf/backend/pydolphin/venv/lib/python3.10/site-packages/requests/sessions.py", line 655, in send r = adapter.send(request, **kwargs) File "/Users/xiaoqiangjiang/source/reddwarf/backend/pydolphin/venv/lib/python3.10/site-packages/requests/adapters.py", line 510, in send raise ProxyError(e, request=request) requests.exceptions.ProxyError: HTTPSConnectionPool(host='chromedriver.storage.googleapis.com', port=443): Max retries exceeded with url: /LATEST_RELEASE_111.0.5563 (Caused by ProxyError('Cannot connect to proxy.', RemoteDisconnected('Remote end closed connection without response'))) python-BaseException Process finished with exit code 1
代理配置代码
s = Service(ChromeDriverManager().install()) option = webdriver.ChromeOptions() # option.add_experimental_option("detach",True) option.add_argument("--proxy-server=127.0.0.1:7890") driver = webdriver.Chrome(service=s,options=option) driver.get("https://accounts.example.com/SignUp") wait = WebDriverWait(driver, 10)
问题原因及解决方法
原因
你仅给Chrome浏览器实例配置了代理,但ChromeDriverManager自动下载chromedriver时,并未使用该代理——它依赖系统网络设置或环境变量的代理配置,而你的代理可能未允许Python程序访问chromedriver.storage.googleapis.com,或代理对该域名的请求处理异常。
解决方法
方法1:给ChromeDriverManager配置代理
让ChromeDriverManager下载时使用指定代理,修改代码如下:
from webdriver_manager.chrome import ChromeDriverManager from webdriver_manager.core.proxy import Proxy, ProxyType from selenium.webdriver.chrome.service import Service from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait # 配置代理 proxy = Proxy( proxy_type=ProxyType.HTTP, http_proxy="127.0.0.1:7890", https_proxy="127.0.0.1:7890" ) # 带代理参数初始化ChromeDriverManager s = Service(ChromeDriverManager(proxy=proxy).install()) option = webdriver.ChromeOptions() option.add_argument("--proxy-server=127.0.0.1:7890") driver = webdriver.Chrome(service=s, options=option) driver.get("https://accounts.example.com/SignUp") wait = WebDriverWait(driver, 10)
方法2:手动下载chromedriver并指定路径
跳过ChromeDriverManager的自动下载,手动下载对应Chrome版本(111.0.5563)的arm64架构chromedriver(适配M1 Mac),直接指定本地路径:
from selenium.webdriver.chrome.service import Service from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait # 替换为你本地chromedriver的实际路径 s = Service("/Users/yourname/path/to/chromedriver") option = webdriver.ChromeOptions() option.add_argument("--proxy-server=127.0.0.1:7890") driver = webdriver.Chrome(service=s, options=option) driver.get("https://accounts.example.com/SignUp") wait = WebDriverWait(driver, 10)
方法3:检查代理配置
确认你的代理工具允许访问chromedriver.storage.googleapis.com,部分代理会默认过滤域名,需手动加入允许列表;同时确保代理工具允许Python程序的流量通过(部分代理仅默认放行浏览器流量)。
内容的提问来源于stack exchange,提问作者Dolphin
相关产品推荐
相关产品推荐

