首页 > 解决方案 > 带有 MS Edge 浏览器的 Python (Selenium):“连接中止。”、ConnectionResetError(10054, ...)“Microsoft Web 驱动程序已停止工作”

问题描述

每次我运行我的 python 代码时,Edge 浏览器都会打开但不会加载 URL。然后 Windows 系统显示一个对话框:

Microsoft Web 驱动程序已停止工作。

一个问题导致程序停止正常工作。如果有可用的解决方案,Windows 将关闭该程序并通知您。

我的 Python 代码:(在 PyCharm 上运行):

# Locate the web driver for Edge browser
driver = webdriver.Edge(executable_path = 'C:/Users/username/webdrivers/MicrosoftWebDriver.exe')

# Open the website
driver.get("https://google.com")

PyCharm 的警告/错误消息:

回溯(最后一次调用):文件“02042020.py”,第 14 行,在驱动程序 = webdriver.Edge(executable_path = 'C:/Users/username/webdrivers/MicrosoftWebDriver.exe') 文件“C:\Users\username \PycharmProjects\Web_Scraping\venv\lib\site-packages\selenium\webdriver\edge\webdriver.py",第 61 行,在init RemoteWebDriver 中。init(文件“C:\Users\username\PycharmProjects\Web_Scraping\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py”,第 157 行,在init self.start_session(capabilities, browser_profile) 文件“C:\Users\username\PycharmProjects\Web_Scraping\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py”,第 252 行,在 start_session 响应 = self.execute (Command.NEW_SESSION,参数)文件“C:\Users\username\PycharmProjects\Web_Scraping\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py”,第 319 行,执行响应 = self.command_executor。执行(driver_command,params)文件“C:\Users\username\PycharmProjects\Web_Scraping\venv\lib\site-packages\selenium\webdriver\remote\remote_connection.py”,第 374 行,在执行中返回 self._request(command_info[ 0], url, body=data) 文件 "C:\Users\username\PycharmProjects\Web_Scraping\venv\lib\site-packages\selenium\webdriver\remote\remote_connection.py",第 402 行,在 _request resp = http.request(method, url, body=body, headers=headers) 文件“C:\Users\username\PycharmProjects\Web_Scraping\venv\lib\site-packages\urllib3\request.py” ,第 79 行,在请求中返回 self.request_encode_body(文件“C:\Users\username\PycharmProjects\Web_Scraping\venv\lib\site-packages\urllib3\request.py”,第 171 行,在 request_encode_body 中返回 self.urlopen(方法, url, **extra_kw) 文件“C:\Users\username\PycharmProjects\Web_Scraping\venv\lib\site-packages\urllib3\poolmanager.py”,第 330 行,在 urlopen 响应 = conn.urlopen(method, u. request_uri, **kw) 文件“C:\Users\username\PycharmProjects\Web_Scraping\venv\lib\site-packages\urllib3\connectionpool.py”,第 719 行,在 urlopen 重试 = retries.increment(文件“C:\Users\username\PycharmProjects\Web_Scraping\venv\lib\site-packages\urllib3\util\retry.py",第 400 行,增量 raise Six.reraise(type(error), error, _stacktrace) File "C:\ Users\username\PycharmProjects\Web_Scraping\venv\lib\site-packages\urllib3\packages\six.py”,第 734 行,在 reraise raise value.with_traceback(tb) 文件“C:\Users\username\PycharmProjects\Web_Scraping\ venv\lib\site-packages\urllib3\connectionpool.py",第 665 行,在 urlopen httplib_response = self._make_request( 文件 "C:\Users\username\PycharmProjects\Web_Scraping\venv\lib\site-packages\urllib3\connectionpool .py”,第 421 行,在 _make_request 中 6.raise_from(e, None) 文件“”,第 3 行,在 raise_from 文件“C:\Users\username\PycharmProjects\Web_Scraping\venv\lib\site-packages\urllib3\connectionpool.py”,第 416 行,在 _make_request httplib_response = conn.getresponse() 文件“C:\Users\username\AppData\Local\Programs \Python\Python38\lib\http\client.py”,第 1322 行,在 getresponse response.begin() 文件“C:\Users\username\AppData\Local\Programs\Python\Python38\lib\http\client.py ",第 303 行,开始版本,状态,原因 = self._read_status() 文件 "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\http\client.py",第 264 行,在_read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") 文件 "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\socket.py",第 669 行,在 readinto 中返回 self._sock.recv_into(b)第 416 行,在 _make_request httplib_response = conn.getresponse() 文件“C:\Users\username\AppData\Local\Programs\Python\Python38\lib\http\client.py”,第 1322 行,在 getresponse response.begin()文件“C:\Users\username\AppData\Local\Programs\Python\Python38\lib\http\client.py”,第 303 行,开始版本,状态,原因 = self._read_status() 文件“C:\Users \username\AppData\Local\Programs\Python\Python38\lib\http\client.py",第 264 行,在 _read_status 行 = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")文件“C:\Users\username\AppData\Local\Programs\Python\Python38\lib\socket.py”,第 669 行,在 readinto 中返回 self._sock.recv_into(b)第 416 行,在 _make_request httplib_response = conn.getresponse() 文件“C:\Users\username\AppData\Local\Programs\Python\Python38\lib\http\client.py”,第 1322 行,在 getresponse response.begin()文件“C:\Users\username\AppData\Local\Programs\Python\Python38\lib\http\client.py”,第 303 行,开始版本,状态,原因 = self._read_status() 文件“C:\Users \username\AppData\Local\Programs\Python\Python38\lib\http\client.py",第 264 行,在 _read_status 行 = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")文件“C:\Users\username\AppData\Local\Programs\Python\Python38\lib\socket.py”,第 669 行,在 readinto 中返回 self._sock.recv_into(b)\Users\username\AppData\Local\Programs\Python\Python38\lib\http\client.py”,第 1322 行,在 getresponse response.begin() 文件“C:\Users\username\AppData\Local\Programs\Python \Python38\lib\http\client.py",第 303 行,在开始版本中,状态,原因 = self._read_status() 文件 "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\http \client.py",第 264 行,在 _read_status 行 = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") 文件 "C:\Users\username\AppData\Local\Programs\Python \Python38\lib\socket.py",第 669 行,在 readinto 中返回 self._sock.recv_into(b)\Users\username\AppData\Local\Programs\Python\Python38\lib\http\client.py”,第 1322 行,在 getresponse response.begin() 文件“C:\Users\username\AppData\Local\Programs\Python \Python38\lib\http\client.py",第 303 行,在开始版本中,状态,原因 = self._read_status() 文件 "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\http \client.py",第 264 行,在 _read_status 行 = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") 文件 "C:\Users\username\AppData\Local\Programs\Python \Python38\lib\socket.py",第 669 行,在 readinto 中返回 self._sock.recv_into(b)在开始版本中,状态,原因 = self._read_status() 文件“C:\Users\username\AppData\Local\Programs\Python\Python38\lib\http\client.py”,第 264 行,在 _read_status 行 = str( self.fp.readline(_MAXLINE + 1), "iso-8859-1") 文件 "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\socket.py",第 669 行,在 readinto返回 self._sock.recv_into(b)在开始版本中,状态,原因 = self._read_status() 文件“C:\Users\username\AppData\Local\Programs\Python\Python38\lib\http\client.py”,第 264 行,在 _read_status 行 = str( self.fp.readline(_MAXLINE + 1), "iso-8859-1") 文件 "C:\Users\username\AppData\Local\Programs\Python\Python38\lib\socket.py",第 669 行,在 readinto返回 self._sock.recv_into(b)

urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(10054, '现有连接被远程主机强行关闭', None, 10054, None))

环境细节:

我尝试了故障排除,但没有奏效。

我的故障排除:

启用将安装适当版本的 WebDriver 的开发人员模式。在 Windows 上:

Settings > Update & Security > For developers > select "Developer Mode".

以管理员身份运行 CMD 以通过 DISM 安装:

DISM.exe /Online /Add-Capability /CapabilityName:Microsoft.WebDriver~~~~0.0.1.0

它说:

Deployment Image Servicing and Management tool
Version: 10.0.18362.1

Image Version: 10.0.18363.592

[==========================100.0%==========================]
The operation completed successfully.

我多次重复这些步骤,但从未奏效。我下载了 Chrome 的网络驱动程序,并将我的 Python 代码切换为在 Chrome 浏览器上运行,它可以正常工作。不幸的是,问题在于,对于这个特定任务,Microsoft Edge 是必需的浏览器。有人可以建议吗?提前致谢!

标签: pythonseleniumselenium-webdrivermicrosoft-edgeselenium-edgedriver

解决方案


出现此问题的原因可能是您使用的 Microsoft WebDriver 版本与 Microsoft Edge 浏览器版本不匹配。据我所知,当我们通过 DISM 安装 Microsoft Edge webdriver 时,默认情况下,我们可以从以下文件夹中找到 webdriver:

"C:\\Windows\\SysWOW64\\MicrosoftWebDriver.exe"

"C:\\Windows\\System32\\MicrosoftWebDriver.exe",

您可以检查它并尝试在代码中使用此路径。


推荐阅读