c# - 抛出异常: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");
}
}
}
获得以下异常:
解决方案
如您的图像所示,您已正确安装了引用,但引用似乎仍然存在配置问题,您需要声明命名空间,然后查看它是否出现错误,
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 包管理器并再次下载程序集引用
推荐阅读
- c# - 无法使用 FTP 在文本文件中传递希伯来语
- angular - 获取 Jenkins BUILD_NUMBER 变量并在 Angular 应用程序中显示它
- javascript - 如何在 html2canvas 中将 dom 下载为 svg?
- magento - Magento 1.9 - Mage::log() 没有创建日志文件
- vba - 删除文件夹中未在 Excel 电子表格中找到的文件
- php - 如何删除奏鸣曲管理类中的操作?
- java - DynamoDB 继承没有映射父哈希
- git - 如何用主分支历史覆盖开发分支
- c# - 从 Ubuntu 使用 C# 访问网络路径
- c# - 参数化 SQL 命令以更改包含的 DB 用户的密码