c# - 如何设置 selenium 3.0,在 C# 中出现错误 "The geckodriver.exe file does not exist..."

标签 c# selenium selenium-webdriver webdriver selenium3

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

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

试图搜索一些示例,但没有找到任何适用于 c# 的东西,仅适用于 java,但仍然无法使其工作。

我的网络驱动程序设置:

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

最佳答案

从 selenium 3.0 开始,您必须为 Firefox 浏览器使用 geckodriver

从这里下载最新的 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

关于c# - 如何设置 selenium 3.0,在 C# 中出现错误 "The geckodriver.exe file does not exist...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40657443/

相关文章:

python - 如何通过 Selenium 和 Python 输入文本搜索并从搜索结果中检索值

python - 如何使用 Selenium 和 Python 提取下拉菜单所选选项的文本

javascript - 如何在创建文件的同一文件中调用 this.function

java - 如何在Appium中获取和存储列表数据以及如何点击特定的搜索记录

python-3.x - 如何使用 Selenium 和 Python 登录 reddit

c# - WinRT 元素渲染错误

java - 从具有相同类名的两个下拉列表中的第一个选项中选择一个选项

c# - 如何使用 C# 在 JavaScript 中创建的自定义对象上调用方法?

c# - 在另一个表达式中计算 C# 表达式

c# - Closedxml:无法下载文件并立即通过邮件发送