selenium chromedriver headless on vps有时会抛出ConnectionResetError:[Errno 104]连接由同行重置

时间:2018-02-18 04:10:03

标签: python-3.x selenium-webdriver debian vps

我有这个python脚本:

options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')  

driver = webdriver.Chrome("/usr/bin/chromedriver", chrome_options=options)

driver.set_window_size(300, 500)

wait = WebDriverWait(driver, 30)

如果我在我的电脑上运行它可以正常工作

问题是当我在vps上使用它时,有时它会抛出这一堆错误:

    Traceback (most recent call last):
  File "acad.py", line 31, in <module>
    driver = webdriver.Chrome("/usr/bin/chromedriver", chrome_options=options)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 310, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 466, in execute
    return self._request(command_info[0], url, body=data)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/remote_connection.py", line 490, in _request
    resp = self._conn.getresponse()
  File "/usr/lib/python3.6/http/client.py", line 1331, in getresponse
    response.begin()
  File "/usr/lib/python3.6/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.6/http/client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.6/socket.py", line 586, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer

我在/ usr / bin上有chromedriver,chrome在/ usr / bin / google-chrome-stable下

任何想法????

1 个答案:

答案 0 :(得分:0)

你可能会尝试一些事情。

我个人发现Firefox&#39; geckogriver对我来说更可靠。我发现网上的其他人报告了同样的事情。下面是我用于geckodriver的脚本。

此外,正如其他人在评论中提到的那样,内存管理对于任一驱动程序都至关重要。如果您的代码抛出异常,操作系统将不会自动销毁资源(它将其视为打开的Web浏览器)。您必须非常小心,始终使用driver.quit()语句退出。一个很好的模式是使用python&#39;和#39;声明。以上的组合让我的问题消失了。

[我的连接脚本]

headless = True

options = webdriver.FirefoxOptions()
if "Linux" in platform.system():
    path = "path/to/geckodriver"
else:
    path = "path/to/geckodriver.exe")

if headless:
    options.add_argument("-headless")
driver = webdriver.Firefox(executable_path=path, firefox_options=options)
driver.implicitly_wait(10)