How to set Selenium webdriver.gecko.driver setting in C#

6.6k Views Asked by At

Straight out of the box I can't get selenium grid to work. I have selenium server 3.4.0 and the following code produces the error shown

Error received

An unhandled exception of type 'System.InvalidOperationException' occurred in WebDriver.dll

Additional information: The path to the driver executable must be set by the 
webdriver.gecko.driver system property; for more information, see 
https://github.com/mozilla/geckodriver. The latest version can be downloaded 
from https://github.com/mozilla/geckodriver/releases

Simple code

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var url = new Uri("http://10.6.122.49:5555/wd/hub");

            var options = new FirefoxOptions();
            options.SetPreference("webdriver.gecko.driver", @"D:\geckodriver.exe");

            var driver = new RemoteWebDriver(url, options.ToCapabilities());

            driver.Navigate().GoToUrl("http://www.google.com");
        }
    }
}

Update

enter image description here

3

There are 3 best solutions below

2
On

Add an environment variable with the name : webdriver.gecko.driver and value =D:\geckodriver.exe under the system variables. and you can safely remove

options.SetPreference("webdriver.gecko.driver", @"D:\geckodriver.exe");

Hope this helps

0
On

I have explained here with clear example, make your changes accordingly

NOTE: Make sure of following this before starting.

  1. If you are using GeckoDriver v0.19.0 use Firefox version 55.0 (and greater) and Selenium 3.5 (and greater) Also make sure to download the right version of GeckoDriver e.g. windows 32 or windows 64 bit for your operating system.

You can download Geckodriver from this location https://github.com/mozilla/geckodriver/releases

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Support.UI;
using NUnit.Framework;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;


namespace ClassLibrary2
{
    public class Class1
    {

        FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"D:\dev1"); // location of the geckodriver.exe file

        [Test]
        public void GeckoDriverImplementation()
        {

            IWebDriver driver = new FirefoxDriver(service);
            driver.Navigate().GoToUrl("https://google.com/");

        }
    }
}
0
On

in C#

 Environment.SetEnvironmentVariable("webdriver.gecko.driver", @"C:\full\path\to\geckodriver.exe");