Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Headless Firefox in Selenium C#

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

like image 204
Biswajit Chopdar Avatar asked Oct 20 '17 11:10

Biswajit Chopdar


People also ask

How do I make Firefox headless in Selenium?

Headless Execution Firefox Driver It is quite simple to run your tests in the headless mode. You need simply to add the "--headless" argument to the FirefoxOptions object. * However, the current official version of Mozilla Firefox is 56.

How do I run headless in Firefox?

Firefox in headless mode, can be run once we configure the geckodriver path. We shall then use the FirefoxOptions class, and send the headless knowledge to the browser with setHeadless method and pass true as a parameter to it.

Does Selenium support headless browser?

Selenium supports headless browser testing using HtmlUnitDriver. HtmlUnitDriver is based on java framework HtmlUnit and is the one of the lightweight and fastest among all headless browser.

How do I use Selenium in headless mode?

You can run Google Chrome in headless mode simply by setting the headless property of the chromeOptions object to True. Or, you can use the add_argument() method of the chromeOptions object to add the –headless command-line argument to run Google Chrome in headless mode using the Selenium Chrome web driver.


1 Answers

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

like image 168
Equalsk Avatar answered Sep 28 '22 05:09

Equalsk