繁体   English   中英

将Selenium与ChromeDriver和Chrome通过Python结合使用时,“连接异常中止”,ConnectionResetError(104,“对等连接重置”)

[英]'Connection aborted.', ConnectionResetError(104, 'Connection reset by peer') using Selenium with ChromeDriver and Chrome through Python

下面的代码循环执行,其中打开10-15个本地.html文件,每个文件的图像另存为.png。

  • Ubuntu服务器16.04
  • ChromeDriver 2.41.578700
  • 谷歌浏览器74.0.3729.108
  • 硒3.141.0
  • Python 3.6

打开前两个文件并保存图像,但是其余结果为:

(“连接已终止。”,ConnectionResetError(104,“对等方重置连接”))

文件的路径都是正确的,并且更改要保存的图像的顺序没有什么不同。

def _save_image(html_file_path, png_file_path, h=850, w=833):
    try:
        from selenium import webdriver
        from selenium.webdriver.chrome.options import Options
    except Exception as ex:
        raise Exception("Saving the plot as a .PNG requires *selenium* package to be installed. Please install selenium using *pip install selenium*.")

    options = Options()
    options.add_argument('--headless')
    options.add_argument('disable-infobars')
    options.add_argument('--disable-extensions')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    #options.add_argument('--disable-gpu')

    if os.name == 'nt':
        chrome_driver_path = os.path.dirname(__file__)
        chrome_driver_path = chrome_driver_path[:-3] + "chromedriver.exe"
    elif os.name == 'posix':
        chrome_driver_path = "/usr/bin/chromedriver"
    else:
        raise Exception("OS could not be detected, thus selenium could not be initialised properly.")
    driver = webdriver.Chrome(chrome_driver_path, chrome_options=options)
    driver.set_window_size(w, h)
    driver.get("file://"+html_file_path)
    time.sleep(5)
    driver.save_screenshot(png_file_path + ".png")
    driver.quit()
    time.sleep(5)

添加了time.sleep(5)来检查错误是否是由于页面加载时间太长所致,并将其增加到30秒,结果是相同的。 由于技术要求,导入语句在功能内,将在稍后阶段对其进行排序。

此错误消息...

('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

...暗示ChromeDriver无法与WebBrowser通信,即Chrome浏览器会话。

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

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

支持Chrome v67-69

支持Chrome v74

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


您可以在Selenium和Heroku中找到详细的讨论:urllib3.exceptions.ProtocolError :(“连接中止。”,ConnectionResetError(104,“对等方重置连接”))

这很可能是因为您将旧版本的chrome驱动程序用于新的Chrome版本。

此处下载适用于您的Chrome的最新chrome驱动程序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM