【问题标题】:C# selenium webdriver raise InvalidOperationException ocurred in WebDriver.dllC# selenium webdriver 在 WebDriver.dll 中引发 InvalidOperationException
【发布时间】:2016-06-16 22:08:39
【问题描述】:

我正在尝试使用 selenium 驱动程序进行自动化测试。

我已经安装了这个 NuGet 包:

我的简单代码和错误:

这是我的项目结构与参考:

我不知道问题出在什么地方?

更多信息

  • VS Professional 2013,版本 12.0.40629.00 更新 5
  • .NET Framework 版本 4.6.01.0.55
  • Selenium.WebDriver 2.53.0
  • WebDriverChromeDriver 2.10
  • 谷歌浏览器版本 51.0.2704.84 m

查看详情...

有来自错误 System.InvalidOperationException 的查看详细信息:

还有堆栈跟踪消息:

   at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
   at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
   at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
   at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options)
   at OpenQA.Selenium.Chrome.ChromeDriver..ctor()
   at SeleniumFirst.Program.Main(String[] args) in c:\Users\roberto.cardenas\Documents\Visual Studio 2013\Projects\SeleniumFirst\SeleniumFirst\Program.cs:line 17
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

【问题讨论】:

  • 您是否添加了 ChromeDriver.exe 引用?
  • 我需要添加作为参考吗?或者我认为该程序在程序运行的同一目录中寻找chromedriver.exe?我错了吗?
  • 我不记得了,但我认为它会在路径环境变量中查找 exe 或某个用户​​建议的。但是如果你点击 viewdetails 然后获取异常调用堆栈,那应该会有所帮助。

标签: c# selenium selenium-webdriver automated-tests


【解决方案1】:

chromedriver.exe 需要位于应用程序目录中,并且专有输出为“始终复制”。

另一种方法是在创建对象时向 chromedriver 构造函数发送路径。这允许您将所有驱动程序放在当前项目的目录中。例如:

var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Drivers";
var chromeDriver = new ChromeDriver(path);

// It will search in "[...]bin\Debug[or Release]\Drivers\chromedriver.exe"

【讨论】:

  • 感谢您的回答我将驱动程序从应用程序目录中复制出来,以便重用驱动程序,也因为我不想将驱动程序包含在我的代码中。但再次感谢您的帮助。
  • 是的,您可以对任何物理路径执行此操作。我的示例是,如果您想在解决方案中创建一个文件夹,其中包含您想要的所有驱动程序,例如 chrome,即 phanton 等
【解决方案2】:

看来您需要更新 chrome 驱动程序。这是一个错误https://bugs.chromium.org/p/chromedriver/issues/detail?id=1257 最新版本是2.21

【讨论】:

  • 谢谢@Vitali,问题已解决。我用的是2.10版本,最新的是2.20。
【解决方案3】:

好的,问题出在 chromedriver.exe 文件上。我使用 NuGet 包管理器安装了 2.10 版,但我正在阅读此 post 并执行了这些步骤。但如果为了避免帖子链接断开的答案,有解决方案:

因此,要解决此问题,您只需导航至 http://chromedriver.storage.googleapis.com/index.html 并下载最新的稳定版本:

在我的情况下是 2.20,所以我导航到:http://chromedriver.storage.googleapis.com/index.html?path=2.20/

后来我把这个驱动版本复制到C:目录并创建驱动路径:

class Program
{
    static void Main(string[] args)
    {
        string DRIVER_PATH = @"C:\";
        //create a reference to our browser
        IWebDriver chrome = new ChromeDriver(DRIVER_PATH);

        //navigate to google page
        chrome.Navigate().GoToUrl("http://www.google.com");

        IWebElement search = chrome.FindElement(By.Name("q"));

        search.SendKeys("executeautomation");
    }
}

一切正常。

【讨论】:

    猜你喜欢
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 2017-11-30
    • 2013-09-13
    • 2018-03-09
    相关资源
    最近更新 更多