【问题标题】:Python Selenium "Connection refused" error is raised in ssh terminal but not in regular terminalPython Selenium“连接被拒绝”错误在 ssh 终端中出现,但在常规终端中没有
【发布时间】:2019-05-11 21:20:52
【问题描述】:

我正在尝试使用 PuTTY 通过 SSH 启动一个包含 selenium webdriver 用法的 python 脚本。在 SSH 终端中运行命令“sudo python3.4 [filepath].py”时,会引发“连接被拒绝”错误,但在服务器终端上运行相同的命令时,它会正常工作。

服务器是运行 Raspbian 的 Raspberry pi,并使用 geckodriver 作为 webdriver。使用 SSH 连接的计算机正在运行 Windows 10。

我尝试在运行 python 脚本之前输入“export DISPLAY=:0”。我还尝试通过 gnome-terminal 运行命令,希望它会强制 pi 加载 gui,因为我认为 selenium 驱动程序是图形驱动程序可能是问题的一部分。

我对 SSH:ing 非常陌生,对 selenium 还是很陌生。

为了使它比我的实际程序更简单,这只是一个启动 selenium 的脚本,它仍然显示相同的问题。

from selenium import webdriver

driver = webdriver.Firefox(executable_path=r'/home/pi/Downloads/gecko3/geckodriver')
driver.get('www.google.com')

driver.quit()

输出(进入我的 SSH 客户端)如下:

Traceback (most recent call last):
  File "/home/pi/Desktop/webtest.py", line 3, in <module>
    driver = webdriver.Firefox(executable_path=r'/home/pi/Downloads/gecko3/geckodriver')
  File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
    keep_alive=True)
  File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused

任何帮助将不胜感激

【问题讨论】:

  • 请检查这个可能的解决方案selenium.common.exceptions.WebDriverException: Message: connection refused 是否解决了您的问题?
  • 在 ARM 上避免 Selenium/Python/Firefox 组合,切换到 Chrome。 Firefox 和 Geckodriver 大多是错误的。如果您需要从源代码构建 Geckodriver,那么您就是 SOL,因为 Rust/Cargo 在 ARM 上存在构建错误。 (根据经验。我花了几个星期试图让 Selenium/Python/Firefox 在 ARM 上按预期工作。我最终放弃并切换到 Chrome)。

标签: python linux selenium selenium-webdriver ssh


【解决方案1】:

我昨天遇到了完全相同的问题,它与安全性有关。监控 gecodriver/firefox/python 的运行时进程,并确保运行时用户是您所期望的。在我的情况下,它作为 IUSR (IIS) 运行,当我将其更改为 NETWORK_SERVICES 时它运行良好。 这是因为它试图在 windows 目录中创建临时配置文件。你可能有类似的问题。

您可能还需要创建私人资料并在您的代码中使用它

ffprofile = webdriver.FirefoxProfile(r"C:..\p1s7i6ei.webScraper") WD = webdriver.Firefox(capabilities=ffCap, options=WDOptions, service_log_path=os.devnull, firefox_profile=ffprofile)

https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 2019-07-04
    • 2017-03-23
    • 2019-04-13
    • 1970-01-01
    • 2018-03-09
    • 2018-03-18
    相关资源
    最近更新 更多