简体   繁体   English

在ssh终端中引发Python Selenium“拒绝连接”错误,但在常规终端中未引发

[英]Python Selenium “Connection refused” error is raised in ssh terminal but not in regular terminal

I am trying to launch a python script, containing selenium webdriver usage, over SSH using PuTTY. 我正在尝试使用PuTTY通过SSH启动一个包含selenium webdriver使用情况的python脚本。 When running the command "sudo python3.4 [filepath].py" in the SSH terminal the "connection refused" error is raised, but when running the same command on the server terminal it works as it should. 在SSH终端上运行命令“ sudo python3.4 [filepath] .py”时,会引发“连接被拒绝”错误,但是在服务器终端上运行相同的命令时,它将正常运行。

The server is a Raspberry pi running Raspbian and is using geckodriver for the webdriver. 该服务器是运行Raspbian的Raspberry pi,并且正在使用geckodriver作为webdriver。 The computer connecting to is using SSH is running Windows 10. 使用SSH连接的计算机正在运行Windows 10。

I have tried typing "export DISPLAY=:0" before running the python script. 我尝试在运行python脚本之前输入“ export DISPLAY =:0”。 I also tried running the command trough gnome-terminal, hoping it would force the pi into loading the gui as I thought that the selenium driver being a graphical one might be part of the problem. 我还尝试通过gnome-terminal运行命令,希望它会迫使pi加载gui,因为我认为硒驱动程序是图形驱动程序可能是问题的一部分。

I am very new to SSH:ing and still fairly new to selenium. 我对SSH:ing非常陌生,对硒还是很陌生。

To make it simpler than my actual program, this is just a script to launch selenium which still shows the same problem. 为了使其比我的实际程序更简单,这只是启动selenium的脚本,仍然显示相同的问题。

from selenium import webdriver

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

driver.quit()

The output (coming into my SSH-client) is as follows: 输出(进入我的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

Any help would be greatly appreciated 任何帮助将不胜感激

I had the exact same issue yesterday and it is related to security. 昨天我遇到了完全相同的问题,它与安全性有关。 monitor your runtime process for gecodriver/firefox/python and make sure the runtime user is what you expect. 监控gecodriver / firefox / python的运行时进程,并确保运行时用户符合您的期望。 in my case it was running as IUSR (IIS), when I changed it to NETWORK_SERVICES it worked fine. 就我而言,它以IUSR(IIS)的形式运行,当我将其更改为NETWORK_SERVICES时,它运行良好。 This is because it tried to create temp profile in the windows directory. 这是因为它试图在Windows目录中创建临时配置文件。 you might have similar issue. 您可能有类似的问题。

you might also have to create private profile and use it in your code 您可能还需要创建私有配置文件并在代码中使用它

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM