How to make Firefox headless programmatically in Selenium with Python?


I am running this code with python, selenium, and firefox but still get ‘head’ version of firefox:

binary = FirefoxBinary('C:\Program Files (x86)\Mozilla Firefox\firefox.exe', log_file=sys.stdout)
self.driver = webdriver.Firefox(firefox_binary=binary)

I also tried some variations of binary:

binary = FirefoxBinary('C:\Program Files\Nightly\firefox.exe', log_file=sys.stdout)
Asked By: Tintinabulator Zea



To invoke Firefox Browser headlessly, you can set the headless property through Options() class as follows:

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

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options, executable_path=r'C:UtilityBrowserDriversgeckodriver.exe')
print ("Headless Firefox Initialized")

There’s another way to accomplish headless mode. If you need to disable or enable the headless mode in Firefox, without changing the code, you can set the environment variable MOZ_HEADLESS to whatever if you want Firefox to run headless, or don’t set it at all.

This is very useful when you are using for example continuous integration and you want to run the functional tests in the server but still be able to run the tests in normal mode in your PC.

$ MOZ_HEADLESS=1 python test # testing example in Django with headless Firefox


$ export MOZ_HEADLESS=1   # this way you only have to set it once
$ python test functional/tests/directory
$ unset MOZ_HEADLESS      # if you want to disable headless mode

Steps through YouTube Video


How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?

Answered By: undetected Selenium

My answer:

set_headless(headless=True) is deprecated.

options.headless = True

works for me

Answered By: Jack Suze

The first answer does’t work anymore.

This worked for me:

from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium import webdriver

options = FirefoxOptions()
driver = webdriver.Firefox(options=options)
Answered By: Philippe Delteil
Used below code to set driver type based on need of Headless / Head for both Firefox and chrome:

// Can pass browser type 

if brower.lower() == 'chrome':
    driver = webdriver.Chrome('..driverschromedriver')
elif brower.lower() == 'headless chrome':
    ch_Options = Options()
    driver = webdriver.Chrome('..driverschromedriver',options=ch_Options)
elif brower.lower() == 'firefox':
    driver = webdriver.Firefox(executable_path=r'..driversgeckodriver.exe')
elif brower.lower() == 'headless firefox':
    ff_option = FFOption()
    driver = webdriver.Firefox(executable_path=r'..driversgeckodriver.exe', options=ff_option)
elif brower.lower() == 'ie':
    driver = webdriver.Ie('..driversIEDriverServer')
    raise Exception('Invalid Browser Type')
Answered By: rahul rai

To the OP or anyone currently interested, here’s the section of code that’s worked for me with firefox currently:

opt = webdriver.FirefoxOptions()
ffox_driver = webdriver.Firefox(executable_path='pathtogeckodriver', options=opt)
Answered By: mana
from selenium.webdriver.firefox.options import Options

if __name__ == "__main__":
    options = Options()
    driver = Firefox(executable_path='geckodriver', firefox_options=options) 
    wait = WebDriverWait(driver, timeout=10)

Tested, works as expected and this is from Official – Headless Mode | Mozilla

Answered By: Abhijit Kokane

Nowadays with this code:

options                 = Options()
options.headless        = True
driver                  = webdriver.Firefox(executable_path=GeckoDriverManager().install(),options=options)

We have a warning:

DeprecationWarning: executable_path has been deprecated, please pass in a Service object

Changing to this one, works perfectly:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# selenium drivers:
# pip3 install selenium
# pip3 install webdriver-manager
# for custom firefox installation: link firefox to /usr/bin/firefox, example: ln -s /opt/firefox/firefox-bin /usr/bin/firefox

from selenium                           import webdriver
from webdriver_manager.firefox          import GeckoDriverManager
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service

options                 = Options()
options.headless        = True
service                 = Service(executable_path=GeckoDriverManager().install())
driver                  = webdriver.Firefox(service=service, options=options)

print("Headless Firefox Initialized")
Answered By: ZiTAL