问题描述
下面的代码循环执行,其中打开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:
- 您正在使用 chromedriver = 2.41
- chromedriver = 2.41 的发行说明明确提到以下内容:
- 您正在使用 chrome = 74.0
- ChromeDriver v74.x 明确提到以下内容:
- 将 ChromeDriver 升级到当前的 ChromeDriver v74.0.3729.6 级.
- 保持 Chrome 版本处于 Chrome v74 级别.(根据ChromeDriver v74.0.3729.6发行说明)
- 始终在
tearDown(){}
方法中调用driver.quit()
以关闭&优雅地销毁 WebDriver 和 Web Client 实例. - 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()
withintearDown(){}
method to close & destroy the WebDriver and Web Client instances gracefully.
因此 ChromeDriver v2.41 与 Chrome浏览器v74.0
这篇关于将Selenium与ChromeDriver和Chrome通过Python结合使用时,“连接已中止",ConnectionResetError(104,“对等方重置连接")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!