2

Now I want to use selenium for some automation of my website. I have pip-installed selenium and downloaded and unzipped the Firefox binary file. The problem now is that whenever I try to initialize the webdriver, for some reason it cannot use the Firefox driver's binary. Below is my code. Unfortunately, I do not have write access, so I cannot move the driver's binary to /usr/local/bin or any other similar directory. So I need to store the driver's binary in a custom directory.

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

options = FirefoxOptions()
options.binary_location = r'geckodriver'
s = Service(executable_path = r'geckodriver')
driver = webdriver.Firefox(service = s, options = options)

This raises the error:

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

So please tell me what did I mess and how to solve this problem. Thanks in advance

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

1

Using Selenium v4.6 and above you don't need the firefox driver's binary explicitly.


Solution

Selenium Manager takes care of downloading the compatible GeckoDriver version. So your effective code block will be:

from selenium import webdriver
from selenium.webdriver import FirefoxOptions

options = FirefoxOptions()
options.binary_location = '\path\to\firefox'
driver = webdriver.Firefox(options = options)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Well something really strange happened when I tried this code (with passing the binary_location to the webdriver options), it got me the same error: ``` from selenium import webdriver from selenium.webdriver import FirefoxOptions options = FirefoxOptions() options.add_argument('--headless') firefox_driver = '/Applications/Firefox' options.binary_location = firefox_driver driver = webdriver.Firefox(options = options) driver.get('https://google.com') print(driver.title) ``` But when I hashed the options.binary_location line it worked. What exactly happened? – Omar Oshiba Jul 10 '23 at 13:25
  • @OmarOshiba `'/Applications/Firefox'` is not really the location of _firefox_driver_. firefox_driver i.e [GeckoDriver](https://stackoverflow.com/a/45510453/7429447) is managed by Selenium Manager now. – undetected Selenium Jul 10 '23 at 14:03
  • @OmarOshiba Glad to be able to help you. [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – undetected Selenium Jul 10 '23 at 14:04
  • Thanks for the explanation. I will tell you what I've done, and tell me what I missed. I downloaded the geckodriver and unzipped it. It got me the geckodriver binary. Now when I modify the binary location to be '/Users/omarmarouf/Desktop/SeleniumEXP/geckodriver' Which is the location of it on my machine. It says: selenium.common.exceptions.InvalidArgumentException: Message: binary is not a Firefox executable Although this is the actual location of the geckodriver which is working as well. So what is the problem and how to solve it? – Omar Oshiba Jul 10 '23 at 14:34
1

You may try this way (assuming you're using selenium 4)

from selenium.webdriver import Firefox, FirefoxOptions
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager

options = FirefoxOptions()
options.binary = r'C:\Program Files\Mozilla Firefox\firefox.exe' # on Windows

driver = Firefox(service=FirefoxService(GeckoDriverManager().install()), options=options)
driver.get('https://www.google.com')

you need to install the webdriver-manager:

pip install webdriver-manager

On a Linux server, the installation location of Firefox can vary depending on how it was installed. If Firefox is installed system-wide,

Here are some common paths where you can find the Firefox executable on Linux:

  • /usr/bin/firefox
  • /usr/lib/firefox/firefox
  • /opt/firefox/firefox

reference: https://pypi.org/project/webdriver-manager/#use-with-firefox

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24