【问题标题】:Exception thrown: 'OpenQA.Selenium.WebDriverException' in WebDriver.dll抛出异常:WebDriver.dll 中的“OpenQA.Selenium.WebDriverException”
【发布时间】:2020-03-06 02:53:23
【问题描述】:

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

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

每次。

注意:我使用的是 Win 10、Visual Studio 企业版 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 包:

截图

【问题讨论】:

  • 使用错误堆栈跟踪更新问题
  • 您是否正确添加了引用?展开并显示您的参考资料和 webdriver 版本。
  • 是的,我已经安装了所有参考:参考截图:

标签: c# selenium selenium-webdriver


【解决方案1】:

您的图片显示您已正确安装参考, 但似乎仍然存在参考配置问题, 您需要声明命名空间,然后查看它是否出错,

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# - All (chrome, firefox, and internet explorer) webdrivers unable to start driver service

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

【讨论】:

  • 是的 Chrome 浏览器打开了,但是打开浏览器后没有任何反应。
  • 好的,让我再试一次
【解决方案2】:

下面是一个示例测试类以及我如何创建驱动程序

driver = new OpenQA.Selenium.Chrome.ChromeDriver();

从我的 NuGet 包中,我使用 Selenium.Chrome.WebDriver 2.41.0、Selenium.WebDriver.ChromeDriver 和 Selenium.WebDriver 3.14、Selenium.Support 3.14

using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

namespace MyTest.UITest
{
    class Program
{
    static void Main(string[] args)
    {

        //Create the reference
        IWebDriver driver =new OpenQA.Selenium.Chrome.ChromeDriver();
        driver.Navigate().GoToUrl("http://www.google.com");
        IWebElement ele = driver.FindElement(By.Id("gs_htif0"));
        ele.SendKeys("Execute Automation");
    }
}

}

既然你不相信我;)

【讨论】:

  • 有兼容性问题吗??
  • 您是否像我一样从 NuGet 添加了所有包?
  • 是的,我做到了。可以参考截图
  • 是的,我已完全复制/粘贴。这就是我得到的,这就是为什么我挠头我所缺少的。我正在使用 Win 10 和 Visual Studio Enterprise 2015 Update 3。只是非常开始,我只受困于此。
【解决方案3】:

很抱歉给您带来不便,我的浏览器和我正在使用的驱动程序之间存在兼容性问题。使用兼容的驱动程序和浏览器,它可以正常工作。感谢大家的帮助。

【讨论】:

    【解决方案4】:

    我也遇到了同样的问题,无法继续。在错误列表中,我遇到了几个错误,它试图复制 chrome 驱动程序文件,但由于其他进程正在使用它而无法访问它。我相信它在之前的运行中没有正确处理,它打开了几个控制台会话并且没有关闭,关闭那些刚刚解决了这个问题的会话。谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-24
      • 2017-06-30
      • 2012-01-24
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 2017-01-22
      • 2011-02-25
      相关资源
      最近更新 更多