python - urllib3.exceptions.ProtocolError : ('Connection aborted.' , 错误 (10054, 'An existing connection was forcibly closed by the remote host' ))

标签 python selenium google-chrome selenium-chromedriver urllib3

我正在尝试使用 Python Selenium chromedriver 在 chrome 上打开一个网站。 Chrome 浏览器正在打开(带有警告)但 url 未打开。

版本详情:Chrome:68.0.3440.106 Selenium :3.14.0 Chrome 驱动程序:2.20 python :2.7

我正在使用下面的代码:

import time
from selenium import webdriver
import selenium
driver = webdriver.Chrome("C:/Python27/chromedriver.exe")
driver.get("https://vancouver.craigslist.ca/")
print(driver.title)
time.sleep(8)
driver.quit()

我遇到以下错误:

C:\Users\sohil7777\PycharmProjects\temp.py\venv\Scripts\python.exe C:/Users/sohil7777/.PyCharmCE2018.2/config/scratches/scratch.py
Traceback (most recent call last):
  File "C:/Users/sohil7777/.PyCharmCE2018.2/config/scratches/scratch.py", line 6, in <module>
    driver = webdriver.Chrome("C:/Python27/chromedriver.exe")
  File "C:\Python27\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 75, in __init__
    desired_capabilities=desired_capabilities)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 156, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 251, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 318, in execute
    response = self.command_executor.execute(driver_command, params)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 375, in execute
    return self._request(command_info[0], url, body=data)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 397, in _request
    resp = self._conn.request(method, url, body=body, headers=headers)
  File "C:\Python27\lib\site-packages\urllib3\request.py", line 72, in request
    **urlopen_kw)
  File "C:\Python27\lib\site-packages\urllib3\request.py", line 150, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "C:\Python27\lib\site-packages\urllib3\poolmanager.py", line 322, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "C:\Python27\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\Python27\lib\site-packages\urllib3\util\retry.py", line 367, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "C:\Python27\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "C:\Python27\lib\site-packages\urllib3\connectionpool.py", line 377, in _make_request
    httplib_response = conn.getresponse(buffering=True)
  File "C:\Python27\Lib\httplib.py", line 1121, in getresponse
    response.begin()
  File "C:\Python27\Lib\httplib.py", line 438, in begin
    version, status, reason = self._read_status()
  File "C:\Python27\Lib\httplib.py", line 394, in _read_status
    line = self.fp.readline(_MAXLINE + 1)
  File "C:\Python27\Lib\socket.py", line 480, in readline
    data = self._sock.recv(self._rbufsize)
urllib3.exceptions.ProtocolError: ('Connection aborted.', error(10054, 'An existing connection was forcibly closed by the remote host'))

我错过了什么吗?非常感谢您的帮助

最佳答案

这个错误信息...

urllib3.exceptions.ProtocolError: ('Connection aborted.', error(10054, 'An existing connection was forcibly closed by the remote host'))

...表示 ChromeDriver 无法启动/生成新的 WebBrowser,即 Chrome 浏览器 session 。

您的主要问题是您使用的二进制文件版本之间的不兼容,如下所示:

  • 您正在使用 chromedriver=2.20
  • chromedriver=2.20 的发行说明明确提到以下内容:

Supports Chrome v43-48

Supports Chrome v67-69

因此 ChromeDriver v2.33Chrome 浏览器 v65.0 之间存在明显的不匹配

解决方案

  • ChromeDriver 升级到当前 ChromeDriver v2.41水平。
  • Chrome 版本保持在 Chrome v67-69 级别之间。 ( as per ChromeDriver v2.41 release notes )
  • 清理您的项目工作区,通过您的IDE重建您的项目,仅使用所需的依赖项。
  • 如果您的基础 Web Client 版本太旧,则将其卸载并安装最新的 GA 和发布版本的 Web Client
  • 执行你的@Test

关于python - urllib3.exceptions.ProtocolError : ('Connection aborted.' , 错误 (10054, 'An existing connection was forcibly closed by the remote host' )),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51799251/

相关文章:

python - 在 Python 中将值附加到字典

mysql - 使用Docker创建 "restorable"MySQL数据库进行UI测试

java - 如何在新项目中重用 Eclipse 启动文件

javascript - 为什么 Mozilla Firefox 会抛出语法错误?

javascript - 将输出回复到输入

python - 将一个字典的元素与另一字典中的值范围进行比较

python - 带元组键的 Max 函数 [更新]

java - 在 Saucelabs 中使用 Selenium 远程 Firefox Webdriver 安装扩展时出现问题

android - 可安装的网络应用程序在 Android 上安装时不使用图标

javascript - Chrome 中奇怪的 javascript 内存