SessionNotCreatedException:消息:预期的浏览器二进制位置,但无法在默认位置找到二进制文件,没有“moz:firefoxOptions.binary”

Пав*_*вел 7 python firefox selenium selenium-webdriver geckodriver

当我尝试在 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 
Run Code Online (Sandbox Code Playgroud)

我在 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
Run Code Online (Sandbox Code Playgroud)

Deb*_*anB 8

这个错误信息...

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
Run Code Online (Sandbox Code Playgroud)

...暗示GeckoDriver在尝试启动/生成新的浏览上下文(Firefox 浏览器会话)时无法定位Firefox二进制文件。


原因

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

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

解决方案

可能的解决方案是:

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

  • 如果Firefox安装在自定义位置,则需要通过实例传递firefox二进制文件的绝对路径,如下所示:Options()

    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/')
    
    Run Code Online (Sandbox Code Playgroud)

参考

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


小智 5

它通过传递FirefoxBinary()选项对我有用。

下面的代码对我有用。

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium import webdriver
firefox_binary = FirefoxBinary()
driver = webdriver.Firefox(firefox_binary=firefox_binary)
Run Code Online (Sandbox Code Playgroud)

上面的代码肯定适合每个人。


归档时间:

查看次数:

10288 次

最近记录:

4 年,7 月 前