I’m working on a C# automation project using Selenium WebDriver to test a web application. The script runs perfectly when using a regular browser window, but I get timeout errors when switching to headless mode.
The error occurs when trying to locate an element:
var loginField = driver.FindElement(By.Id("username_input"));
This throws a WebDriverException with a 60-second timeout. I’ve tested with Chrome, Firefox, and other headless browsers but get the same result.
Here’s my simplified code:
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace WebTester
{
class Application
{
static void Main(string[] args)
{
var service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
var chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--headless");
IWebDriver webDriver = new ChromeDriver(service, chromeOptions);
webDriver.Navigate().GoToUrl("https://example-site.com/");
webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
Thread.Sleep(3000);
var loginButton = webDriver.FindElement(By.Id("login_btn"));
loginButton.Click();
webDriver.Close();
webDriver.Quit();
}
}
}
Why does the same element detection work in normal browser mode but fail in headless mode? Any suggestions would be helpful.
I encountered this exact problem last year during a regression testing project. The root cause was that headless mode often loads pages faster than regular mode, but JavaScript execution can be slower or behave differently. What solved it for me was adding explicit waits instead of relying solely on Thread.Sleep(). Try using WebDriverWait with ExpectedConditions.ElementIsVisible() before attempting to find your element. Also, consider adding the --disable-gpu and --no-sandbox arguments to your ChromeOptions, as these can resolve rendering issues in headless environments. Another thing to check is whether your target website has anti-bot detection that specifically targets headless browsers - some sites block or delay responses when they detect headless user agents.
check if your site uses lazy loading or has different dom structure in headless mode. some elements might not render the same way without a visible viewport. try adding --disable-web-security and --disable-features=VizDisplayCompositor to your chrome options - helped me with similar timeouts on angular apps that loaded differently headless vs normal mode.
Had similar issues with a financial application testing suite where headless mode consistently failed on dynamic content. The problem turned out to be viewport size differences - headless browsers default to unusual dimensions which can affect element positioning and visibility. Try explicitly setting window size with chromeOptions.AddArgument("--window-size=1920,1080") before the headless argument. Additionally, some applications use different CSS or JavaScript logic based on whether they detect a “real” browser session. I found success by adding --disable-blink-features=AutomationControlled and setting a proper user agent string to make the headless session appear more legitimate. The combination of proper viewport sizing and these stealth options resolved timeout issues that only occurred in headless mode for our test automation framework.