Selenium Python:使用Chrome配置文件时驱动初始化异常导致后续操作失败
Selenium Python:使用Chrome配置文件时驱动初始化异常导致后续操作失败
我之前写了一段Python Selenium代码,目的是在多台Windows用户电脑上直接调用Chrome默认配置文件(这样用户无需重新登录Google账号),这段代码运行了一段时间都很稳定:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service import os chrome_options = Options() user_data_dir = os.path.expanduser("~\\AppData\\Local\\Google\\Chrome\\User Data").replace("\\","/") chrome_options.add_argument(f"--user-data-dir={user_data_dir}") chrome_options.add_argument("--profile-directory=Default") chrome_options.add_argument("--remote-debugging-pipe") chrome_options.add_argument("--enable-logging") chrome_options.add_argument("--log-level=0") service = Service(executable_path="chromedriver.exe") driver = webdriver.Chrome(service=service, options=chrome_options) driver.get('http://www.stackoverflow.com')
问题出现的变化
之前我的Chrome浏览器版本是133,但配套用的是ChromeDriver131——虽然运行时会弹出几条USB相关的错误日志,但驱动仍能正常初始化,后续的页面操作也能正常执行。
最近我把ChromeDriver升级到133以匹配Chrome版本,结果现在运行代码时,Chrome窗口能正常打开并加载默认配置文件,但后续任何操作都无法执行,错误日志也变多了,最终会抛出超时异常。
完整错误日志
[15784:6412:0219/110942.360:ERROR:usb_descriptors.cc(146)] Failed to read length for configuration 1. [15784:6412:0219/110942.360:ERROR:usb_descriptors.cc(105)] Failed to read all configuration descriptors. Expected 2, got 1. [15784:6412:0219/110942.361:ERROR:device_event_log_impl.cc(199)] [11:09:42.362] USB: usb_device_win.cc:95 Failed to read descriptors from \\?\usb#vid_0bda&pid_8153#000001#{a5dcbf10-6530-11d2-901f-00c04fb951ed}. [15784:9928:0219/110942.399:ERROR:connection_factory_impl.cc(434)] Failed to connect to MCS endpoint with error -111 Created TensorFlow Lite XNNPACK delegate for CPU. [15784:9928:0219/110943.601:ERROR:registration_request.cc(291)] Registration response error message: DEPRECATED_ENDPOINT [15784:9928:0219/111004.433:ERROR:registration_request.cc(291)] Registration response error message: DEPRECATED_ENDPOINT [2264:7264:0219/111012.233:ERROR:ssl_client_socket_impl.cc(877)] handshake failed; returned -1, SSL error code 1, net_error -100 [2264:7264:0219/111012.400:ERROR:ssl_client_socket_impl.cc(877)] handshake failed; returned -1, SSL error code 1, net_error -100 [15784:9928:0219/111012.400:ERROR:connection_factory_impl.cc(434)] Failed to connect to MCS endpoint with error -100 [15784:9928:0219/111052.482:ERROR:registration_request.cc(291)] Registration response error message: DEPRECATED_ENDPOINT [15784:9928:0219/111053.654:ERROR:connection_factory_impl.cc(434)] Failed to connect to MCS endpoint with error -111 [15784:9852:0219/111151.138:ERROR:devtools_pipe_handler.cc(188)] Connection terminated while reading from pipe Traceback (most recent call last): File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\connectionpool.py", line 534, in _make_request response = conn.getresponse() File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\connection.py", line 516, in getresponse httplib_response = super().getresponse() File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\http\client.py", line 1428, in getresponse response.begin() ~~~~~~~~~~~~~~^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\http\client.py", line 331, in begin version, status, reason = self._read_status() ~~~~~~~~~~~~~~~~~^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\http\client.py", line 292, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\socket.py", line 719, in readinto return self._sock.recv_into(b) ~~~~~~~~~~~~~~~~~~~~^^^ TimeoutError: timed out The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Users\MyUser\Documents\svg\test.py", line 24, in <module> driver = webdriver.Chrome(service=service, options=chrome_options) File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__ super().__init__( ~~~~~~~~~~~~~~~~^ browser_name=DesiredCapabilities.CHROME["browserName"], ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<3 lines>... keep_alive=keep_alive, ^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 66, in __init__ super().__init__(command_executor=executor, options=options) ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 250, in __init__ self.start_session(capabilities) ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 342, in start_session response = self.execute(Command.NEW_SESSION, caps)["value"] ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 427, in execute response = self.command_executor.execute(driver_command, params) File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 404, in execute return self._request(command_info[0], url, body=data) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 428, in _request response = self._conn.request(method, url, body=body, headers=headers, timeout=self._client_config.timeout) File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\_request_methods.py", line 143, in request return self.request_encode_body( ~~~~~~~~~~~~~~~~~~~~~~~~^ method, url, fields=fields, headers=headers, **urlopen_kw ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\_request_methods.py", line 278, in request_encode_body return self.urlopen(method, url, **extra_kw) ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\poolmanager.py", line 443, in urlopen response = conn.urlopen(method, u.request_uri, **kw) File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\connectionpool.py", line 841, in urlopen retries = retries.increment( method, url, error=new_e, _pool=self, _stacktrace=sys.exc_info()[2] ) File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\util\retry.py", line 474, in increment raise reraise(type(error), error, _stacktrace) ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\util\util.py", line 39, in reraise raise value File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\util\util.py", line 39, in reraise raise value File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen response = self._make_request( ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\util\util.py", line 39, in reraise raise value File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\util\util.py", line 39, in reraise raise value ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\util\util.py", line 39, in reraise ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\util\util.py", line 39, in reraise ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\util\util.py", line 39, in reraise raise value File "C:\Users\MyUser\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen response = self._make_request( conn, ...<10 lines>
可尝试的解决方法
1. 彻底关闭Chrome相关进程
如果有其他Chrome窗口或后台进程占用默认配置文件,会导致驱动无法正常建立连接。打开任务管理器,结束所有chrome.exe和chromedriver.exe进程后,再重新运行代码。
2. 替换远程调试参数
新版ChromeDriver对--remote-debugging-pipe的兼容性可能存在问题,尝试换回传统的端口调试模式:
# 替换原有的--remote-debugging-pipe参数 chrome_options.add_argument("--remote-debugging-port=9222")
3. 复制配置文件到新路径
系统默认的Chrome用户数据目录可能存在权限限制,尝试将AppData\Local\Google\Chrome\User Data复制到一个非系统目录(比如C:\Temp\ChromeUserData),然后修改代码中的路径:
user_data_dir = "C:/Temp/ChromeUserData"
4. 添加显式等待
升级后Chrome加载配置文件的时间可能变长,添加显式等待确保驱动完全初始化:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By driver = webdriver.Chrome(service=service, options=chrome_options) # 等待页面body元素加载完成,最多等待10秒 WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body"))) driver.get('http://www.stackoverflow.com')
备注:内容来源于stack exchange,提问作者Deniz Terzioglu
相关产品推荐
相关产品推荐

