【问题标题】:selenium chromedriver headless on vps sometimes throws ConnectionResetError: [Errno 104] Connection reset by peervps 上的 selenium chromedriver headless 有时会抛出 ConnectionResetError: [Errno 104] Connection reset by peer
【发布时间】:2018-07-28 15:17:03
【问题描述】:

我有这个 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 下

有什么想法吗????

【问题讨论】:

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


【解决方案1】:

您可以尝试几件事。

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

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

[我的连接脚本]

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 2019-04-30
    • 2019-09-14
    • 2020-01-15
    • 2022-08-16
    • 2016-06-21
    相关资源
    最近更新 更多