11

I am having trouble with a selenium error related to a FireFox binary.

I added C:/Users/Mack/AppData/Local/Programs/Python/ to PATH using backslashes and rebooted. I downloaded what I thought is the correct file here https://github.com/mozilla/geckodriver/releases I put the file in the directory connected to PATH.

To remedy this: I tried using two backslashes

binary = FirefoxBinary("C:\\Users\Mack\AppData\Local\Programs\Python\Python38-32\geckodriver-v0.27.0-win64\geckodriver.exe")

which throws the same error

I tried using one backslash

binary = FirefoxBinary("C:\Users\Mack\AppData\Local\Programs\Python\Python38-32\geckodriver-v0.27.0-win64\geckodriver.exe")

which throws:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
Traceback (most recent call last):
  File "C:\Users\Mack\Desktop\hacker-stories\Trends.py", line 32, in <module>
    browser = webdriver.Firefox(**firefox_binary=binary**)
  File "C:\Users\Mack\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 170, in __init__
    RemoteWebDriver.__init__(
  File "C:\Users\Mack\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\Mack\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\Mack\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Mack\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: binary is not a Firefox executable

Code Test.py

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary("C:/Users/Mack/AppData/Local/Programs/Python/Python38-32/geckodriver-v0.27.0-win64/geckodriver.exe")
print(binary)
browser = webdriver.Firefox(firefox_binary=binary)

options = Options()
options.set_preference("browser.download.folderList",2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir","/Data")
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream,application/vnd.ms-excel")
driver = webdriver.Firefox(firefox_options=options)

Any help understanding this error is much appreciated.

3
  • Are you sure that's a firefox binary. Commented Sep 23, 2020 at 19:23
  • As @arundeepchohan mentioned, your argument to FirefoxBinary looks off. Shouldn't that be a path to firefox.exe instead of the path to the geckodriver? Commented Sep 23, 2020 at 19:25
  • stackoverflow.com/questions/57528718/… Here's an example of the proper way to do it. Commented Sep 23, 2020 at 19:37

7 Answers 7

6

This error message...

selenium.common.exceptions.InvalidArgumentException: Message: binary is not a Firefox executable

...implies that the binary file which you have passed as an argument to FirefoxBinary() isn't a valid executable.

You seem to have passed the absolute path of the geckodriver.exe as an argument to FirefoxBinary() which is causing the error.


Solution

Instead of the geckodriver.exe you need to pass the absolute path of the firefox.exe. Moreover, firefox_options is deprecated now and you have to use options and you can use the following solution:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

options = Options()
options.binary = FirefoxBinary(r'C:\Program Files\Mozilla Firefox\firefox.exe')
options.set_preference("browser.download.folderList",2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir","/Data")
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream,application/vnd.ms-excel")
driver = webdriver.Firefox(executable_path=r'C:/Users/Mack/AppData/Local/Programs/Python/Python38-32/geckodriver-v0.27.0-win64/geckodriver.exe', options=options)

References

You can find a couple of relevant detailed discussion in:

Sign up to request clarification or add additional context in comments.

Comments

4

This is how I solved it in 2024

Your selenium, firefox and geckodriver should all support each other and here are the compatible versions:

https://firefox-source-docs.mozilla.org/testing/geckodriver/Support.html

I downgraded the selenium from 4 to 3.11 and upgraded my firefox to version 120. My geckodriver was version 0.33.

Comments

4

The problem for me was that I've installed firefox through snap on my Ubuntu and Selenium was unable to find it/work with it. I had to install Firefox through apt, which was not allowed by default on my Ubuntu. I had to follow this guide: https://www.omgubuntu.co.uk/2022/04/how-to-install-firefox-deb-apt-ubuntu-22-04 and then the problem was resolved ✅

My use case was I had to run Firefox in headless mode on an ubuntu server machine.

7 Comments

same problem for me, thank you
You can make firefox from the snap work with Selenium by setting the firefox binary to /snap/firefox/current/usr/lib/firefox/firefox. You can also use the geckodriver from the snap as /snap/firefox/current/usr/lib/firefox/geckodriver. Depending on your use case, this may be easier than installing a PPA.
the guide doesn't say what file to edit for step 5...
It does @pyCthon. That whole section is a command, and the second part after pipe, specifies what file it is writing into: | sudo tee /etc/apt/preferences.d/mozilla. However I don't remember doing that step. It wasn't reuiqred for me.
BertD, your comment saved me so much time. Thank you. /snap/firefox/current/usr/lib/firefox/firefox is the Firefox binary I needed to use.
|
2

Change the binary to whatever firefox.exe you get and your executable path to your geckodriver.

options = Options()
binary = r'C:\Program Files\Mozilla Firefox\firefox.exe'
options.set_preference("browser.download.folderList",2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir","/Data")
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream,application/vnd.ms-excel")
options.binary = binary
driver = webdriver.Firefox(r'C:/Users/Mack/AppData/Local/Programs/Python/Python38-32/geckodriver-v0.27.0-win64/geckodriver.exe',options=options)

Comments

2

It seems that when using Firefox Portable, the FirefoxPortable.exe file is not recognized, instead, when creating binary path, point to firefox.exe file found under "FirefoxPortable\App\firefox64" (in my case). The above is valid if your Gecko driver works - so the first few lines of output (before the exception) look something like this:

[RemoteTestNG] detected TestNG version 7.3.0
1611580278948   geckodriver INFO    Listening on 127.0.0.1:18391

This issue appeared for me after the problem described here: Cannot find firefox binary in PATH. Make sure firefox is installed thus, they are not the same issue - as presented in some stack comments.

good luck!

Comments

1

For Selenium 4 FirefoxBinaryis deprecated

from selenium import webdriver
from selenium.webdriver.firefox.service import Service

driver_path = f"{settings.BASE_DIR}/geckodriver"

options = webdriver.FirefoxOptions()
options.binary_location = "/usr/lib/firefox"

driver = webdriver.Firefox(service=Service(executable_path=driver_path), options=options)

This code works well for me.

Comments

-1

Your path to the exe looks incorrect.

"/" (slash) is something used often in unix like systems to represent directory structures.

Window uses a backslash.

Change the string to use two backslashes instead of a forward slash.

1 Comment

This does not work ... binary = FirefoxBinary("C:\\Users\Mack\AppData\Local\Programs\Python\Python38-32\geckodriver-v0.27.0-win64\geckodriver.exe")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.