Headless Firefox in Selenium C#

Biswajit Chopdar picture Biswajit Chopdar · Oct 20, 2017 · Viewed 9.5k times · Source

I want to run firefox headless.

Not hide the browser window or open it in a virtual desktop, Firefox supports headless mode by using "-headless" flag.

Problem is I know how to do it in chrome but not in Firefox.

My code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace MyApp {
public partial class Form1: Form {
    public Form1() {
        InitializeComponent();
    }

    private void StartBtn_Click(object sender, EventArgs e) {

        IWebDriver driver;
        FirefoxOptions options = new FirefoxOptions();
        options.AddArguments("--headless");
        driver = new FirefoxDriver(options);
    }
}
}

My WinForm application only has a button with name StartBtn. On clicking of the button Firefox should run headless, but it opens in a normal window.


Update I updated firefox to 56.0.1

Now I get a different error:

An unhandled exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.dll

Additional information: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line

Answer

Equalsk picture Equalsk · Oct 20, 2017

Headless mode in Firefox is supported from version 56 on Windows and Mac OS. Ensure that you have the correct version installed.

https://developer.mozilla.org/en-US/Firefox/Headless_mode#Browser_support

With Firefox v56.0.1, Selenium.WebDriver v3.6.0 and geckodriver v0.19.0 (x64) this works correctly for me.

Regarding the error:

An unhandled exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.dll

Ensure you're using the correct version of geckodriver. I suspect you're using the x32 build on an x64 machine, get the x64 build.

https://github.com/mozilla/geckodriver/releases