当我尝试在 Selenium 中创建 Firefox 实例时,出现以下错误:
Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line
我在 Ubuntu 20.04 上运行。
我该如何修复它?
browser = webdriver.Firefox()
Traceback (most recent call last):
File "key.py", line 10, in <module>
browser = webdriver.Firefox()
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
RemoteWebDriver.__init__(
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line
这个错误信息...
selenium.common.exceptions.SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line
...意味着 GeckoDriver 在尝试启动/生成新的 浏览上下文 即 Firefox 浏览器 会话时无法找到 firefox 二进制文件。
此错误的可能原因可能是以下任一原因:
可能的解决方案是:
如果您的系统中未安装Firefox,则需要在执行测试之前安装它。
如果 Firefox 安装在自定义位置,则需要通过 Options()
实例传递 firefox 二进制文件的
绝对路径,如下所示:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = r"C:/location/to/Firefox/Binary/firefox.exe"
driver = webdriver.Firefox(options=options, executable_path="C:/location/to/geckodriver.exe")
driver.get('http://google.com/')
您可以在以下位置找到一些相关的详细讨论:
它通过传递 FirefoxBinary() 选项对我有用。
下面的代码对我有用。
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium import webdriver
firefox_binary = FirefoxBinary()
driver = webdriver.Firefox(firefox_binary=firefox_binary)
上面的代码肯定适合每个人。