Firefox browser not running in headless mode

I’m a beginner coder and I’m having trouble with headless browsers. I tried Chrome first but ran into errors I couldn’t fix. So I switched to Firefox, which runs without errors, but the browser window still pops up on my screen.

I’m using Windows 10 and Python 3.7. Here’s my code:

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

def search_amazon():
    search_terms = ['book', 'laptop', 'headphones']
    query = random.choice(search_terms)
    
    firefox_options = Options()
    firefox_options.add_argument('--headless')
    
    browser = webdriver.Firefox(executable_path='C:\webdriver\geckodriver.exe', options=firefox_options)
    
    url = f'https://www.amazon.de/s?k={query}'
    browser.get(url)
    
    time.sleep(5)
    browser.quit()

search_amazon()

Can someone help me figure out why the browser is still visible? Thanks!

yo ameliat, had similar probs. try adding this line before creating the browser:

firefox_options.headless = True

worked for me when the --headless arg didnt. also, double-check ur geckodriver path. somtimes windows paths can be tricky. good luck!

Hey there! I’ve dealt with similar headless browser issues before. From what I can see, your code looks mostly correct. However, I noticed you’re using Firefox options instead of Chrome options. Have you made sure you have the latest version of geckodriver installed? Sometimes outdated drivers can cause unexpected behavior.

Another thing to check is your Firefox installation. Make sure it’s up to date and that there are no conflicting settings in your Firefox profile that might be overriding the headless mode.

If those don’t work, you could try adding these additional options:

firefox_options.add_argument(‘–disable-gpu’)
firefox_options.add_argument(‘–no-sandbox’)

These helped me in the past when I was having trouble with headless mode. Let me know if any of these suggestions help!

I’ve encountered this issue before. One thing to check is your Firefox profile settings. Sometimes, existing profiles can interfere with headless mode. Try creating a new Firefox profile specifically for your script.

You can do this by adding:

firefox_options.set_preference(‘profile’, webdriver.FirefoxProfile())

This creates a fresh profile each time the script runs, which might resolve the visibility problem.

Also, ensure you’re using the latest geckodriver and Selenium versions. Outdated components can sometimes cause unexpected behavior with headless mode.

If these don’t work, you might want to consider using a different webdriver like geckodriver-autoinstaller, which can help manage driver versions automatically.