簡體   English   中英

如何在 Raspbian Buster 上使用無頭 Firefox 設置 Selenium

[英]How to setup Selenium with headless Firefox on Raspbian Buster

I have gotten this to work in Jessie/Stretch with previous Firefox versions but can't seem to get it to work with Raspberry Pi 3B or 4 running Raspbian Buster and the latest Selenium, 3.141.0 in Python.

由於 iceweasel 不再可用,我通過 apt 安裝firefox-esr並從https://github.com/mozilla/geckodriver/releases獲取最新的 ARM geckodriver,目前為 0.23.0,然后運行以下代碼:

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

options = Options()
options.headless = True
capabilities = webdriver.DesiredCapabilities().FIREFOX
capabilities["marionette"] = False
driver = webdriver.Firefox(options=options, executable_path='/usr/bin/geckodriver', capabilities=capabilities)
driver.get('https://www.google.com')
print(driver.page_source)
driver.close()
display.stop()

這將返回錯誤:

Message: Can't load the profile. Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+. Profile Dir: /tmp/tmpxMUUcF If you specified a log_file in the FirefoxBinary constructor, check it for details.

Traceback (most recent call last):
  File "SeleniumDebugExample.py", line 50, in <module>
    driver = webdriver.Firefox(options=options, executable_path='/srv/main/geckodriver', capabilities=capabilities)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 191, in __init__
    self.binary, timeout)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 52, in __init__
    self.binary.launch_browser(self.profile, timeout=timeout)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 73, in launch_browser
    self._wait_until_connectable(timeout=timeout)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 114, in _wait_until_connectable
    % (self.profile.path))
WebDriverException: Message: Can't load the profile. Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+. Profile Dir: /tmp/tmpxMUUcF If you specified a log_file in the FirefoxBinary constructor, check it for details.

我正在指定 geckodriver,所以我不確定問題是什么......

如果有人獲得了無頭 selenium 以在 Raspbian Buster 上工作並且可以分享一些可能,將不勝感激!

經過一堆測試后,問題似乎在於添加 marrionette=False 功能...回到 Jessie 和 iceweasel 我需要添加它才能使其工作但似乎不再需要 firefox-esr...我能夠通過以下方式完成此工作:

from selenium import webdriver
from pyvirtualdisplay import Display

driver = webdriver.Firefox(executable_path='/usr/bin/geckodriver')
driver.get('https://www.google.com')
print(driver.page_source)
driver.close()
display.stop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM