【问题标题】:SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary'SessionNotCreatedException:消息:预期的浏览器二进制位置,但无法在默认位置找到二进制文件,没有“moz:firefoxOptions.binary”
【发布时间】:2021-03-02 14:39:53
【问题描述】:

当我尝试在 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

【问题讨论】:

  • 运行 ">whereis firefox" 命令并在 FirefoxOptions 中设置该路径
  • 您需要安装firefox 二进制和geckodriver。把它们放在你的$PATH

标签: python selenium selenium-webdriver firefox geckodriver


【解决方案1】:

此错误消息...

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 二进制文件/em> 会话。


原因

此错误的可能原因可能是以下任一原因:

  • Firefox 未安装在您的系统中。
  • Firefox 未安装在系统的默认位置。

解决方案

可能的解决方案是:

  • 如果您的系统中没有安装 Firefox,您需要在执行测试之前安装它。

  • 如果 Firefox 安装在自定义位置,您需要通过 @987654326 传递 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/')
    

参考文献

您可以在以下位置找到一些相关的详细讨论:

【讨论】:

    猜你喜欢
    • 2021-09-22
    • 2018-03-31
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 2022-06-29
    • 2021-03-26
    • 1970-01-01
    相关资源
    最近更新 更多