如何在C#中设置Selenium webdriver.gecko.driver设置

时间:2017-08-29 15:52:35

标签: selenium-webdriver selenium-firefoxdriver

开箱即用,我无法让硒网格工作。我有selenium服务器3.4.0和以下代码产生显示的错误

收到错误

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

简单代码

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

更新

enter image description here

3 个答案:

答案 0 :(得分:1)

我在这里用清楚的例子解释,相应地做出改变

注意:确保在开始之前执行此操作。

  1. 如果您使用的是GECKODRIVER v0.19.0,请使用Firefox版本55.0(及更高版本)和Selenium 3.5(及更高版本) 另外,请确保根据您的操作系统下载GeckoDriver的正确位,即Windows 32或Windows 64位。
  2. 您可以从此位置下载Geckodriver 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 geckdriver.exe file
    
            [Test]
            public void GeckoDriverImplementation()
            {
    
                IWebDriver driver = new FirefoxDriver(service);
                driver.Navigate().GoToUrl("https://google.com/");
    
            }
        }
    }
    

答案 1 :(得分:0)

在系统变量下添加名称为:webdriver.gecko.driver和value = D:\ geckodriver.exe的环境变量。你可以安全地删除

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

希望这有帮助

答案 2 :(得分:0)

在C#中

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