当我尝试在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没有安装在系统中的默认位置
解决方案
可能的解决方案有:
如果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/')
参考
你可以在中找到一些相关的详细讨论
- InvalidArgumentException:消息:二进制文件不是使用GeckoDriver Firefox Selenium和Python的Firefox可执行错误
- 应为浏览器二进制位置,但在默认位置找不到二进制,未提供"moz:ffirefoxOptions.binary"功能
- 预期的浏览器二进制位置,但在默认位置找不到二进制,没有使用GeckoDriver提供"moz:ffirefoxOptions.binary"功能
它通过传递FirefoxBinary((选项对我有效。
下面的代码对我有效。
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium import webdriver
firefox_binary = FirefoxBinary()
driver = webdriver.Firefox(firefox_binary=firefox_binary)
上面的代码肯定适用于所有人。