无法使用Python和geckodriver在Raspbian下运行selenium-webdriver

时间:2019-06-30 13:20:07

标签: python selenium selenium-webdriver raspbian geckodriver

我正在尝试使硒在树莓派上以无头模式运行。我的脚本可以在Windows计算机上正常运行,但是我无法使这种特殊组合正常工作:

  • Raspbian GNU / Linux 9(拉伸)
  • Python 3.7.3
  • geckodriver 0.23.0(2018-10-04)
  • selenium-server-standalone-3.141.59
  • Mozilla Firefox 52.9.0

首先,我按照 documentation中的说明启动selenium-server-standalone

$ java -jar selenium-server-standalone-3.141.59.jar &
[1] 1842
14:39:26.881 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.59, revision: e82be7d358
14:39:27.321 INFO [GridLauncherV3.lambda$buildLaunchers$3] - Launching a standalone Selenium Server on port 4444
2019-06-30 14:39:27.589:INFO::main: Logging initialized @2114ms to org.seleniumhq.jetty9.util.log.StdErrLog
14:39:28.755 INFO [WebDriverServlet.<init>] - Initialising WebDriverServlet
14:39:29.174 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 4444

然后我在Python中运行以下测试脚本:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from pyvirtualdisplay import Display

display = Display(visible=0, size=(800, 800))
display.start()

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get("http://google.com/")
driver.quit()

我收到以下错误消息:

Traceback (most recent call last):
  File "debug3.py", line 10, in <module>
    driver = webdriver.Firefox(options=options)
  File "/usr/local/opt/python37/lib/python3.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
    keep_alive=True)
  File "/usr/local/opt/python37/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/opt/python37/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/opt/python37/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/opt/python37/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused

根据this线程,我包括了pyvirtualdisplay。但是结果并没有太大的区别。另外,this线程中的代码不起作用。

1 个答案:

答案 0 :(得分:0)

我确定了问题,这是三个错误的组合:

  1. 无需运行selenium-server-standalone
  2. 我没有使用正确版本的geckodriver,因此raspbian不必安装最新版本的Firefox / Iceweasel。 Mozilla 52的正确版本是geckodriver 0.17。兼容版本的列表是here,而geckodriver的发行版是here
  3. 对于xvfb而言,python脚本的运行速度太快,它无需进行渲染就可以进行交互。我还为pyvirtualdisplay安装了其他推荐的依赖项(通过 tell toolbar 1 entire contents end tell ,另请参见here