static void Main()
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"D:\drivers"); service.FirefoxBinaryPath = @"C:\Program Files\Mozilla Firefox\firefox.exe"; var driver = new FirefoxDriver(service); Sometimes a corrupted Firefox profile causes the driver to hang on startup.
Introduction If you are automating Firefox using Selenium WebDriver in C# (or any .NET language), you have likely encountered the dreaded WebDriverException : "Cannot start the driver service on http://localhost..." This error stops your automation dead in its tracks. It means Selenium cannot communicate with the geckodriver (the bridge between your code and Firefox). var service = FirefoxDriverService
var service = FirefoxDriverService.CreateDefaultService(@"C:\path\to\geckodriver"); service.Port = 12345; // Use any free port above 1024 var driver = new FirefoxDriver(service); | Firefox Version | GeckoDriver Version | |----------------|---------------------| | 115+ (ESR) | 0.33+ | | 120+ | 0.34+ | | 130+ | 0.35+ |
// Ensure geckodriver.exe is in the same folder as your .exe or in PATH var options = new FirefoxOptions(); options.AddArgument("--headless"); // optional: headless mode The geckodriver tries to bind to a port
Below, I break down exactly why this happens and the 7 most effective fixes. OpenQA.Selenium.WebDriverException: Cannot start the driver service on http://localhost:xxxx/ ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted. Root Cause Analysis The error indicates a port conflict or driver process failure . The geckodriver tries to bind to a port on localhost , but something is blocking it.
// No need to set driver path anymore var driver = new FirefoxDriver(); // Selenium Manager handles geckodriver Update your NuGet packages to Selenium.WebDriver 4.6 or higher. Let me know in the comments which fix worked for you, or share your geckodriver.log for more help! service.Port = 12345
GitHub mozilla/geckodriver 4. Add Driver Path to System PATH (or specify explicitly) Option A – Add to PATH: Place geckodriver.exe in a folder already on PATH, or add its folder to system environment variables.
var driver = new FirefoxDriver(options); // will search PATH driver.Navigate().GoToUrl("https://example.com"); Console.WriteLine(driver.Title); driver.Quit();
pkill -f geckodriver
taskkill /F /IM geckodriver.exe