Selenium C# automation fails when running Firefox in headless mode

I’m working on a C# application that uses Selenium to automate website testing. The automation runs perfectly when using a regular browser window, but as soon as I switch to headless mode, it stops working.

The problem occurs when trying to locate elements on the page. Here’s the specific line that causes issues:

var loginField = driver.FindElement(By.Id("user_login"));

This throws a timeout exception after 60 seconds, but only in headless mode. I’ve tested with Chrome, Firefox, and PhantomJS browsers, all configured for headless operation, and none of them work.

Here’s my complete code:

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace TestAutomation
{
    class MainApp
    {
        static void Main(string[] args)
        {
            var service = FirefoxDriverService.CreateDefaultService();
            service.HideCommandPromptWindow = true;

            var firefoxOptions = new FirefoxOptions();
            firefoxOptions.AddArguments("--headless");

            IWebDriver webDriver = new FirefoxDriver(service, firefoxOptions);
            webDriver.Url = "https://example-site.com/";
            webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15000);

            System.Threading.Thread.Sleep(3000);

            var loginButton = webDriver.FindElement(By.Id("login_btn"));
            loginButton.Click();

            webDriver.Close();
            webDriver.Quit();

            Console.ReadKey();
        }
    }
}

I’m using Firefox 73.0.1 with Geckodriver 0.26.0. Any suggestions on what might be causing this headless mode issue?

Had the same issue last month. Add --disable-dev-shm-usage to your firefoxOptions - it fixes memory problems that mess up element detection. Your timeout’s way too high (15000 seconds??). Drop it to 10-20 seconds max. In headless mode, the DOM needs extra time to load, so throw in an explicit wait before clicking.

Had the same nightmare with headless Firefox. The problem is headless mode doesn’t load CSS and JavaScript the same way - some elements won’t render or load async. Set a proper window size with firefoxOptions.AddArguments("--width=1920", "--height=1080") because headless defaults to tiny viewports. Add WebDriverWait with ExpectedConditions.PresenceOfElementLocated() before finding elements to make sure the page actually loads. Responsive CSS can change the page structure in headless mode. Try adding --disable-gpu and --no-sandbox for better stability. Also check if the element ID exists by comparing driver.PageSource in headless vs regular mode.

I’ve hit this same issue with headless Firefox. The problem’s usually timing - headless mode renders pages differently, which messes with element loading. First thing I noticed: your ImplicitWait is set to 15000 seconds. That’s way too high and probably causing issues. Drop it to 10-30 seconds max. Try adding explicit waits with WebDriverWait and ExpectedConditions for the elements you’re targeting. Way more reliable than implicit waits. Also, some sites actively detect headless browsers and either change behavior or block you completely. Add a user-agent header to your FirefoxOptions to make it look like a regular browser session. Turn on logging too - it’ll show you what’s actually happening during execution. That’s how I figured out that certain JavaScript wasn’t running properly in headless mode on specific sites.