gpt4 book ai didi

python - 如何在 Raspbian Buster 上使用 headless Firefox 设置 Selenium

转载 作者:行者123 更新时间:2023-12-01 06:59:37 27 4
gpt4 key购买 nike

我已经让它在 Jessie/Stretch 中与以前的 Firefox 版本一起工作,但似乎无法让它与运行 Raspbian Buster 和最新的 Selenium(Python 中的 3.141.0)的 Raspberry Pi 3B 或 4 一起工作。

由于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,所以我不确定问题是什么......

如果有人已经在 Raspbian Buster 上使用了 headless Selenium ,并且可以分享一些,我们将不胜感激!

最佳答案

经过一系列测试后,问题似乎出在添加 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()

关于python - 如何在 Raspbian Buster 上使用 headless Firefox 设置 Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58697952/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com