![](/img/trans.png)
[英]Python script is failing with ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
[英]'Connection aborted.', ConnectionResetError(104, 'Connection reset by peer') using Selenium with ChromeDriver and Chrome through Python
下面的代碼循環執行,其中打開10-15個本地.html文件,每個文件的圖像另存為.png。
打開前兩個文件並保存圖像,但是其余結果為:
(“連接已終止。”,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瀏覽器會話。
您的主要問題是所使用的二進制版本之間的不兼容性 ,如下所示:
支持Chrome v67-69
支持Chrome v74
因此, ChromeDriver v2.41和Chrome瀏覽器v74.0之間存在明顯的不匹配
tearDown(){}
方法內調用driver.quit()
,以優雅地關閉和破壞WebDriver和Web Client實例。 您可以在Selenium和Heroku中找到詳細的討論:urllib3.exceptions.ProtocolError :(“連接中止。”,ConnectionResetError(104,“對等方重置連接”))
這很可能是因為您將舊版本的chrome驅動程序用於新的Chrome版本。
從此處下載適用於您的Chrome的最新chrome驅動程序
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.