Firefox browser window appears despite headless mode configuration

I’m having trouble with Firefox headless mode setup

I’m a beginner with Python and Selenium. When I try to run Firefox in headless mode, the browser window still shows up on my screen even though I think I configured it correctly.

I’m using Python 3.8 on Windows 11. Here’s my code:

import time, random, threading
from selenium import webdriver
from selenium.webdriver.firefox.options import Options as FF_Options

def start_automation():
    threading.Timer(30., start_automation).start()
    keywords = (apple, banana, orange)
    selected_word = random.choice(keywords)
    
    ff_options = FF_Options()
    ff_options.add_argument("--headless")
    
    browser = webdriver.Firefox(executable_path="D:\\drivers\\geckodriver.exe")
    target_url = "https://www.ebay.com/sch/i.html?_nkw=" + str(selected_word)
    browser.get(target_url)
    time.sleep(3)
    browser.close()

start_automation()

What am I doing wrong? The browser keeps opening visibly instead of running in the background.

Had the exact same problem when I started using Selenium automation. The other answers are spot on about passing the options parameter, but I’ll add that you should also consider using the newer Service approach instead of executable_path since it’s being deprecated. Your options object is correctly configured with the headless argument, but Firefox simply ignores it when you don’t include options=ff_options in the webdriver initialization. I’ve made this mistake countless times myself - you set everything up properly but forget that one crucial parameter. Once you fix that, your automation will run completely in the background as intended.

you forgot to pass the options to the webdriver! change webdriver.Firefox(executable_path="D:\\drivers\\geckodriver.exe") to webdriver.Firefox(executable_path="D:\\drivers\\geckodriver.exe", options=ff_options) - thats why its ignoring your headless setting

The issue is quite simple – while you’ve set the Firefox options, you haven’t applied them when initializing the webdriver. In the current setup, you’re creating the ff_options object and adding the headless argument, but when you create the Firefox webdriver, you’re only supplying the executable path. You must include the options parameter as well. The correct line should be browser = webdriver.Firefox(executable_path="D:\\drivers\\geckodriver.exe", options=ff_options). Failing to pass the options means Firefox will disregard your headless setting and launch with a visible window, which is a common error among newcomers to Selenium.