I am attempting to write C# code that will launch Mozilla Firefox, browse to a website and automate form entry. I can get this to function correctly without being headless, but now I am looking to convert my code to run a headless Firefox Browser.
The following code will function if the latest version of Selenium and Firefox driver is installed via NuGet and also the latest version of the geckodriver is at the appropriate folder location.
What needs to be done to make this code open a headless Mozilla Firefox?
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
namespace GoogleSearch
{
class LaunchFirefox
{
static void Main(string[] args)
{
//Start Firefox Gecko Driver Service from Custom Location
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"C:\GoogleSearch");
//Force the CMD prompt window to automatically close and suppress diagnostic information
service.HideCommandPromptWindow = true;
service.SuppressInitialDiagnosticInformation = true;
//Launch Mozilla Firefox from custom location
service.FirefoxBinaryPath = @"C:\Firefox\firefox.exe";
//Initialize new Firefox Driver with the above service arguments
FirefoxDriver driver = new FirefoxDriver(service);
//Navigate to the Google website
driver.Navigate().GoToUrl("https://www.google.com");
//Automate custom Google Search Submission
driver.FindElement(By.Name("q")).SendKeys("Stack Overflow");
}
}
}
I tried inserting Firefox options, but that option doesn't seem to be available.
I get the following error when I attempt adding options to the firefox driver initialization line:
Error CS1503 Argument 2: cannot convert from 'OpenQA.Selenium.Firefox.FirefoxOptions' to 'OpenQA.Selenium.Firefox.FirefoxProfile'
Any assistance would be appreciated.
I am running the following software:
- Windows 7
- Visual Studio Community Edition 2017
- Mozilla Firefox 61.0.1
- Gecko Driver 0.21.0