如何设置selenium 3.0,在c#中收到错误“geckodriver.exe文件不存在...”

问题描述 投票:8回答:2

将visual studio中的selenium更新为3.0,firefox更新为47.0,现在我尝试使用本地webdriver模式时出现此错误:geckodriver.exe文件不存在于当前目录或PATH环境变量的目录中。

当我使用远程模式(seleniumhub)时,即使它使用firefox 45.0版本也能正常工作。

试图搜索一些例子,但没有为c#找到任何东西,只为java而且仍然无法使它工作。

我的webdriver设置:

 switch (ConfigurationManager.AppSettings["WebDriverMode"].ToLower())
                {
                    case "local":
                        switch (ConfigurationManager.AppSettings["WebDriverBrowserCapabilities"].ToLower())
                        {
                            case "firefox":
                                driver = new AdvancedFirefoxDriver();
                                break;
                            case "ie":
                                driver = new AdvancedInternetExplorerDriver();
                                break;
                            case "chrome":
                                driver = new AdvancedChromeDriver();
                                break;
                            default:
                                throw new NotImplementedException(string.Format("WebDriverBrowserCapabilities of \"{0}\" is not implemented for {1} mode", ConfigurationManager.AppSettings["WebDriverBrowserCapabilities"].ToLower(), ConfigurationManager.AppSettings["WebDriverMode"].ToLower()));
                        }

                        break;
                    case "remote":
                        var huburl = new Uri(ConfigurationManager.AppSettings["SeleniumHubAddress"]);
                        DesiredCapabilities capabilities;
                        switch (ConfigurationManager.AppSettings["WebDriverBrowserCapabilities"].ToLower())
                        {
                            case "firefox":
                                capabilities = DesiredCapabilities.Firefox();
                                break;
                            case "ie":
                                capabilities = DesiredCapabilities.InternetExplorer();
                                break;
                            case "chrome":
                                capabilities = DesiredCapabilities.Chrome();
                                break;
                            default:
                                throw new NotImplementedException(string.Format("WebDriverBrowserCapabilities of \"{0}\" is not implemented for {1} mode", ConfigurationManager.AppSettings["WebDriverBrowserCapabilities"].ToLower(), ConfigurationManager.AppSettings["WebDriverMode"].ToLower()));
                        }

                        capabilities.IsJavaScriptEnabled = true;
                        driver = new AdvancedRemoteWebDriver(huburl, capabilities);
                        break;
                    default:
                        throw new NotImplementedException();
                }
c# selenium selenium-webdriver webdriver selenium3
2个回答
7
投票

从selenium 3.0开始,你必须使用geckodriver for Firefox浏览器。

从这里下载最新的geckodriver https://github.com/mozilla/geckodriver/releases

您有两种选择:

  1. 在Windows系统环境变量PATH中输入geckodriver路径。
  2. 或者以编程方式指定geckodriver.exe的位置,如下所示。

System.Environment.SetEnvironmentVariable("webdriver.gecko.driver",@"/path/to/geckodriver.exe"

注意:如果设置PATH环境变量,则可能需要重新启动系统。

从Firefox 47开始(不包括它),Selenium默认使用geckodriver功能。对于47及之前的版本,您可能需要关闭此功能,以便Selenium可以使用Firefox内置支持,就像我们以前使用这些版本一样。

JAVA版本实现相同:

DesiredCapabilities d = new DesiredCapabilities();
d.setCapability("marionette", false);  // to disable marionette.
WebDriver driver = new FirefoxDriver(d);

参考文献:

  1. how to set system properties in C#
  2. https://msdn.microsoft.com/en-us/library/z46c489x.aspx
  3. https://superuser.com/questions/317631/setting-path-in-windows-7-command-prompt
  4. https://stackoverflow.com/a/40466109/2575259

0
投票

我与Selenium的Chrome驱动程序有类似的问题,我正在学习构建自动化框架的课程,我在框架引用下安装了NuGet包,而不是在测试下安装它。 enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.