0

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
sethjbr
  • 125
  • 2
  • 11

2 Answers2

1

Since using Firefox in headless mode is accomplished by passing a --headless option to the Firefox command line, the code you want is similar to the following:

// DISCLAIMER WARNING! The below code was written from
// memory, without benefit of an IDE. It may not be entirely
// correct, and may not even compile without modification.
//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
FirefoxOptions options = new FirefoxOptions();
options.BrowserExecutableLocation = @"C:\Firefox\firefox.exe";
options.AddArgument("--headless");

//Initialize new Firefox Driver with the above service and options
FirefoxDriver driver = new FirefoxDriver(service, options);

//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”);

As long as you’re using 3.14 or later of the .NET bindings, that code, or something similar to it, should work.

JimEvans
  • 27,201
  • 7
  • 83
  • 108
  • Thank you for your response. However, with this code and similar code, I get this error in Visual Studio whenever I try adding options to the FirefoxDriver line: Error CS1503 Argument 1: cannot convert from 'OpenQA.Selenium.Firefox.FirefoxDriverService' to 'OpenQA.Selenium.Firefox.FirefoxBinary' Do you know how to resolve this error? – sethjbr Aug 14 '18 at 15:27
  • Can you validate the version of the .NET bindings you’re using? If you’re using 3.14, at least, the constructor should have that as a valid signature. Prior to 3.13, the constructor taking a `FirefoxDriverService` and a `FirefoxOptions` as arguments didn’t exist. – JimEvans Aug 14 '18 at 22:23
  • My target .NET framework in Visual Studio is 4.6.1 for this solution. Would you have time to write and test a few lines of code that use both a custom firefox location and run headless firefox? I can easily figure out the rest of the code. – sethjbr Aug 15 '18 at 15:22
0

I finally found the answer to this question. The stack overflow page here helped a lot in finding the answer to this question:

Setting BrowserExecutableLocation in FirefoxOptions in Selenium doesn't prevent an "Unable to find a matching set of capabilities" error

Here is my code for headless firefox that works very well when Firefox is used in a non-default location:

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

namespace GoogleSearch
{
    class LaunchFirefox
    {
        static void Main(string[] args)
        {
                //Start Firefox Gecko Driver Service from Custom Location
                    FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"C:\FirefoxDriver");

                    //Force the CMD prompt window to automatically close and suppress diagnostic information
                        service.HideCommandPromptWindow = true;
                        service.SuppressInitialDiagnosticInformation = true;

                        //Initialize Firefox Options class
                            FirefoxOptions options = new FirefoxOptions();

                            //Set Custom Firefox Options
                                options.BrowserExecutableLocation = @"C:\Mozilla Firefox\Firefox.exe";
                                //options.AddArgument("--headless");

            //Start Firefox Driver with service, options, and timespan arguments
                FirefoxDriver driver = new FirefoxDriver(service, options, TimeSpan.FromMinutes(1));

            //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");
        }
    }

}

Hope the helps someone else using C# code and trying to run headless firefox.

Blessings,

Seth

sethjbr
  • 125
  • 2
  • 11