我有一个Python(3.5(脚本,它在无头Debian(9(服务器上使用Selenium(3.12(,chromedriver(2.39(和Google Chrome(67(。
Python安装在虚拟环境中。
大约 50% 的时间脚本运行无故障。另外 50% 我收到以下错误。这只是chrome/chromedriver(两个最新版本都直接来自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
当您使用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')
参考
- 如何配置ChromeDriver以通过Selenium在无头模式下启动Chrome浏览器?
- 运行 Chrome 无头浏览器时遇到问题