Fixing Selenium SessionNotCreatedException Message: Expected browser binary location, but unable to find binary in default location, no ‘moz:firefoxOptions.binary’ capability provided

Selenium SessionNotCreatedException Message: Expected browser binary location, but unable to find binary in default location, no ‘moz:firefoxOptions.binary’ capability provided is an error which occurs when GeckoDriver cannot find the Firefox binary in your system.

My goal today is to provide a clear and detailed explanation of why this error is happening and how to solve it, we will also check out other ways to get rid of this problem for good.

Exploring the Error : Selenium SessionNotCreatedException Message: Expected browser binary location, but unable to find binary in default location, no ‘moz:firefoxOptions.binary’ capability provided

This is an error which occurs when GeckoDriver cannot find the Firefox binary in your system.

Bellow is the error message, please make sure it is the right one.

                                                                       #
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
                                                                       #

Bellow we will describe how the error can be solved. With multiple possible solutions.

The Method that solved my problem : add Geckodriver and Firefox binary to your system PATH ( while Firefox is installed )

First of all, we should understand which situation can lead to this error. The error happens usually when you create a Firefox browser instance in Selenium.

                                                                       #
browser = webdriver.Firefox()
                                                                       #

What happens after this is that Selenium tries to locate the Firefox binaries but It cannot find anything, which results in the error we are trying to solve.

The first option anyone must try is to install Firefox. When Firefox is not installed on your system, the result is going to be this error.

So please, install the Firefox browser before you try the second option.

The error can also happen when you have already installed Firefox and that is exactly why we need the second option.

The second option is to add the Firefox binary. You can either to that inside your code, or you can add the binary to your Path in your system Path.

You can add the binary to your system Path, by first downloading the Firefox binary from the official link :

https://www.selenium.dev/selenium/docs/api/javascript/module/selenium-webdriver/firefox.html

In this post, I am going to suppose that you know how to add a binary to your system PATH.

Also, do not forget to add Geckodriver to your system PATH too.

You can also do that inside your code, just like this. ( do not forget to import Options in the beginning of your code )

                                                                       #
from selenium.webdriver.firefox.options import Options
                                                                       #

This is what you need

                                                                       #
options = Options()
options.binary_location = r"C:/Firefoxlocation/Binary/firefox.exe"
                                                                       #

You can also do it like this.

                                                                       #
binary = FirefoxBinary('C:\yourpath\Firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary)
                                                                       #

You should use the code above after importing FirefoxBinary , like this

                                                                       #
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
                                                                       #

I hope the method above fixed your problem, good luck to you, have an amazing day

Summing-up : 

That is it guys, this is the end of this article aka guide, I hope you found it useful in solving your problem, make sure to support our work on Kofi, you do not have to but hey you can donate to the team.

Thank you for reading my blog post to the end, If you want to learn more about the Python programming language, check out the official Python Documentation : https://docs.python.org/3/