首页 > 解决方案 > 抛出异常:WebDriver.dll 中的“OpenQA.Selenium.WebDriverException”

问题描述

我是使用 C# 的 Selenium 新手。每当我尝试运行简单的代码时,我都会得到:

Exception thrown: 'OpenQA.Selenium.WebDriverException' in WebDriver.dll

每次。

注意:我使用的是 Win 10、Visual Studio Enterprise Edition 2015、Selenium 3x 和 Chrome 最新版本。下面是代码。提前致谢

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Selenium1
{
    class Program
    {
         static void Main(string[] args)
        {
            //Create the reference
            IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl("http://www.google.com");
            IWebElement ele = driver.FindElement(By.Id("gs_htif0"));
            ele.SendKeys("Execute Automation");
        }
    }
}

获得以下异常:

参考附图 参考截图

每次都遇到异常,没有运气..我已经尝试了一切。 在此处输入图像描述

Nuget 包: 在此处输入图像描述

截屏 在此处输入图像描述

标签: c#seleniumselenium-webdriver

解决方案


如您的图像所示,您已正确安装了引用,但引用似乎仍然存在配置问题,您需要声明命名空间,然后查看它是否出现错误,

using OpenQA.Selenium; 
using OpenQA.Selenium.Chrome; // or more specific its ok you get no error till here

我希望选项将成为您的问题,那么您还需要在构造函数中提供一些配置选项,

var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("download.default_directory", "YOUR_DownloadPath");
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");                        
chromeOptions.AddUserProfilePreference("download.prompt_for_download", false);
chromeOptions.AddArguments("disable-infobars");

然后,使用

try{
IWebDriver Driver = new ChromeDriver(chromeOptions);
}catch(Exception e){Console.writeLine(e);}

您需要将代码放入 try catch 块中,因此,您可以获得完整的错误异常跟踪,并将其提供给我们。你有成功打开chrome浏览器吗?好的 chrome 浏览器可以打开,参考这个,可能是重复的问题,你的代码使用新的基于 marionette 的 web 驱动程序而不是 gecko 驱动程序。

Selenium - Visual Studios- C# - 所有(chrome、firefox 和 Internet Explorer)网络驱动程序无法启动驱动程序服务

尝试使用 Nuget 包管理器并再次下载程序集引用


推荐阅读