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?