Search code examples
pythonwindowsselenium-webdriverfirefoxgeckodriver

Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided


I am trying to get back into using Python Webdriver. I have here the code

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\Users\Cody\Downloads\geckodriver.exe')
driver.get('http://inventwithpython.com')

This causes:

C:\Users\Cody\Projects>python accounting.py
Traceback (most recent call last):
  File "accounting.py", line 4, in <module>
    driver = webdriver.Firefox(executable_path=r'C:\Users\Cody\Downloads\geckodriver.exe')
  File "C:\Users\Cody\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\firefox\webdriver.py", line 170, in __init__
    RemoteWebDriver.__init__(
  File "C:\Users\Cody\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\Cody\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\Cody\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Cody\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-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

If I try:

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\Users\Cody\Downloads')
driver.get('http://inventwithpython.com')

I get

C:\Users\Cody\Projects>python accounting.py
Traceback (most recent call last):
  File "C:\Users\Cody\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\common\service.py", line 72, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2032.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2032.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1307, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
PermissionError: [WinError 5] Access is denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "accounting.py", line 4, in <module>
    driver = webdriver.Firefox(executable_path=r'C:\Users\Cody\Downloads')
  File "C:\Users\Cody\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\firefox\webdriver.py", line 164, in __init__
    self.service.start()
  File "C:\Users\Cody\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\common\service.py", line 86, in start
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'Downloads' executable may have wrong permissions.

Geckodriver.exe is sitting right there in the downloads folder.

enter image description here


Solution

  • This error message...

    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
    

    ...implies that the GeckoDriver was unable to locate the executable while trying to initiate/spawn a new Browsing Context i.e. Firefox Browser session.


    Reason

    The two primary reasons for this error are as follows:

    • Firefox isn't installed within your system.
    • Firefox isn't installed at the default location within your system.

    Solution

    The possible solutions are:

    • If Firefox isn't installed in your system, you need to install it before executing your tests.

    • If Firefox is installed at a customized location, you need to pass the absolute path of the firefox binary as follows through an Options() instance:

      from selenium import webdriver
      
      options = webdriver.FirefoxOptions()
      options.binary_location = r"C:/location/to/Firefox/Binary/firefox.exe"
      driver = webdriver.Firefox(executable_path=r'C:\Users\Cody\Downloads\geckodriver.exe', options=options)
      driver.get('http://inventwithpython.com/')
      

    References

    You can find a couple of relevant detailed discussion in: