【发布时间】:2020-03-06 00:35:23
【问题描述】:
我正在学习如何在 C# 中编写 selenium 测试,并在尝试运行下面的测试用例时遇到此错误。它失败了:IWebElement query = driver.FindElement(By.Name("q"));
Test method SeleniumDemo.SearchGoogle.SearchForCheese threw exception:
System.ArgumentException: elementDictionary (Parameter 'The specified dictionary does not contain an element reference')
代码:
[TestMethod]
public void SearchForCheese()
{
using (var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)))
{
driver.Navigate().GoToUrl("http://www.google.com");
// Find text input
IWebElement query = driver.FindElement(By.Name("q"));
// Enter something to search for
query.SendKeys("cheese");
// Submit form
query.Submit();
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(d => d.Title.Contains("cheese"));
Assert.AreEqual(driver.Title, "cheese - Google Search");
};
}
有什么想法吗?提前谢谢!
【问题讨论】:
-
我已经编写了相当一部分的 Selenium 测试,但答案对我来说并不明显,但 this code 和类似的东西似乎是引发该异常的原因。似乎与缺少的元素 ID 有关。不知道这是否有帮助,但它的东西。我个人会调试并单步调试 Selenium 代码。
标签: c# visual-studio testing selenium-webdriver