Selenium无头ChromeDriver出现ConnectionResetError错误:[Errno 104] Connection reset by peer

3
我有一个 Python (3.5) 脚本,在一个无头 Debian (9) 服务器上使用 Selenium (3.12),chromedriver (2.39) 和 Google Chrome (67)。
Python 安装在 virtualenv 中。
大约有50%的时间脚本可以正常运行。另外50%的时间,我会得到以下错误。这只是 chrome/chromedriver 的一个普通 bug(两者都是直接从 Google 获取的最新版本),还是可能与我的设置/代码有关?

我的代码非常标准:

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
尝试降级到3.8版本。 - AntoineLB
谢谢,降级到3.8.0似乎起作用了。 https://dev59.com/CFwY5IYBdhLWcg3wsphj#48983008 - artfulrobot
1个回答

2

如果您使用的是Debian Server并且正在使用Headless 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')  

参考资料


谢谢,我已经添加了所有这些参数,但仍然有50%的几率它能正常工作。 - artfulrobot
谢谢。对我有用。 - Ashok Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接