我正在尝试使用 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
任何帮助将不胜感激
最佳答案
我昨天遇到了完全相同的问题,它与安全性有关。监控 gecodriver/firefox/python 的运行时进程,并确保运行时用户是您所期望的。在我的例子中,它作为 IUSR (IIS) 运行,当我将它更改为 NETWORK_SERVICES 时它工作正常。 这是因为它试图在 windows 目录中创建临时配置文件。你可能有类似的问题。
您可能还必须创建私有(private)配置文件并在您的代码中使用它
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
关于Python Selenium "Connection refused"错误在 ssh 终端中出现,但在常规终端中没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56094356/