我是不熟悉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");
}
}
}
获取以下异常:
答案 0 :(得分: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浏览器可以打开, 引用此内容,可能是重复的问题, 您的代码使用的是新的基于木偶的Web驱动程序,而不是壁虎驱动程序。
尝试使用Nuget程序包管理器并再次下载程序集引用
答案 1 :(得分:0)
下面是一个示例测试类以及如何创建驱动程序
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");
}
}
}
因为,你不相信我;)
答案 2 :(得分:0)
很抱歉,我使用的浏览器和驱动程序之间存在兼容性问题。使用兼容的驱动程序和浏览器,即可正常工作。感谢大家的帮助。
答案 3 :(得分:0)
我也遇到同样的问题,无法继续。在错误列表中,我尝试复制chrome驱动程序文件时遇到了几个错误,但由于其他进程正在使用它而无法访问它。我相信它在上次运行中未得到正确处理,该运行已打开了两个控制台会话,但尚未关闭,仅解决了那些已解决此问题的问题。谢谢。