um与ChromeDriver和Chrome通过Python结合

um与ChromeDriver和Chrome通过Python结合

本文介绍了将Selenium与ChromeDriver和Chrome通过Python结合使用时,“连接已中止",ConnectionResetError(104,“对等方重置连接")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The code below is executed in a loop, where 10-15 local .html files are opened and an image of each is saved as a .png.

  • Ubuntu Server 16.04
  • ChromeDriver 2.41.578700
  • Google Chrome 74.0.3729.108
  • 硒3.141.0
  • Python 3.6

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

The first two files are opened and the image is saved, however the rest result in:

文件的路径都是正确的,并且更改要保存的图像的顺序没有影响.

The paths to the files are all correct and changing the order of the images to be saved does not make a difference.

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秒,结果是相同的.由于技术要求,导入语句在功能内,将在稍后阶段进行排序.

The time.sleep(5) was added to check if the error was due to the page taking long to load, increased it to 30 seconds and the result was the same. The import statements are within the function due to a technical requirement which will be sorted at a later stage.

推荐答案

此错误消息...

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

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

...implies that the ChromeDriver was unable to communicate with the WebBrowser i.e. Chrome Browser session.

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

Your main issue is the incompatibility between the version of the binaries you are using as follows:

  • 您正在使用 chrome = 74.0
  • ChromeDriver v74.x 明确提到以下内容:
  • 因此 ChromeDriver v2.41 Chrome浏览器v74.0

    • Upgrade ChromeDriver to current ChromeDriver v74.0.3729.6 level.
    • Keep Chrome version at Chrome v74 levels. (as per ChromeDriver v74.0.3729.6 release notes)
    • Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.

    这篇关于将Selenium与ChromeDriver和Chrome通过Python结合使用时,“连接已中止",ConnectionResetError(104,“对等方重置连接")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 23:57