Selenium无头ChromeDriver因ConnectionResetError而失败:[Errno 104]连接由同行重置

时间:2018-06-08 13:29:53

标签: python selenium selenium-webdriver selenium-chromedriver google-chrome-headless

我有一个Python(3.5)脚本,在无头Debian(9)服务器上使用Selenium(3.12),chromedriver(2.39)和谷歌Chrome(67)。

Python安装在virtualenv

大约50%的时间脚本运行没有错误。另外50%我得到以下错误。这只是一个简单的ol' chrome / chromedriver中的错误(这两个版本都是谷歌的最新版本)还是可能与我的设置/代码有关?

我的代码非常标准:

options = webdriver.ChromeOptions()                                                                                                                                                                        
options.add_argument('headless')                                                                                                                                                                           
options.binary_location='/usr/bin/google-chrome-stable'                                                                                                                                                                                                                                                                                                                                        
options.add_argument('window-size={}x{}'.format(*self.window_size))                                                                                                                                                                                                                                                                          
self.driver = webdriver.Chrome(chrome_options=options)  

错误消息:

  File "./grab-regulars.py", line 25, in __init__
    self.driver = webdriver.Chrome(chrome_options=options)
  File "cwd/env/lib/python3.5/site-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
    desired_capabilities=desired_capabilities)
  File "cwd/env/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 156, in __init__
    self.start_session(capabilities, browser_profile)
  File "cwd/env/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 245, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "cwd/env/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    response = self.command_executor.execute(driver_command, params)
  File "cwd/env/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 472, in execute
    return self._request(command_info[0], url, body=data)
  File "cwd/env/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 496, in _request
    resp = self._conn.getresponse()
  File "/usr/lib/python3.5/http/client.py", line 1198, in getresponse
    response.begin()
  File "/usr/lib/python3.5/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.5/http/client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.5/socket.py", line 576, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer

1 个答案:

答案 0 :(得分:1)

当您使用 Debian服务器并使用 无头Chrome 时,您需要将 WebDriver 实例配置为如下:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.binary_location='/usr/bin/google-chrome-stable'
options.add_argument('--headless')
options.add_argument('--start-maximized') 
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('window-size={}x{}'.format(*self.window_size))                                                                                                                                                                                                                                                                          
self.driver = webdriver.Chrome(chrome_options=options, executable_path='/path/to/chromedriver')  

参考