【问题标题】:Getting System.ArgumentException when running C# Selenium Test运行 C# Selenium 测试时获取 System.ArgumentException
【发布时间】: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


【解决方案1】:

检查并确保您的 ChromeDriver() 版本与您本地计算机上的版本匹配。

下面是一个 XUnit 测试,它将使用 WebDriverManager NuGet 包重新创建您的问题,如果您的计算机上不可用,您可以手动指定要下载的 webdriver 版本:

    [Fact]
    public void ChromeDriverThrows_ArgumentException_True()
    {
        var google = "https://www.google.com/";
        var message = "elementDictionary (Parameter 'The specified 
                        dictionary does not contain an element 
                         reference')";

        // Specify a different of ChromeDriver than what is installed on machine.
        _ = new DriverManager().SetUpDriver(
                      "https://chromedriver.storage.googleapis.com/2.25/chromedriver_win32.zip",
                     Path.Combine(Directory.GetCurrentDirectory(), "chromedriver.exe"),
                      "chromedriver.exe"
                     );

        var driver = new ChromeDriver();

        driver.Navigate().GoToUrl(google);

        var exception = Assert.Throws<ArgumentException>(() => driver.FindElement(By.Name("q")));

        Assert.True(message == exception.Message, $"{exception.Message}");
    }

如果你选择在你的项目中使用WebDriverManager,你可以使用下面的自动设置来自动下载一个与你机器上安装的Chrome版本匹配的chromedriver.exe,这样你就不必手动管理它们了:

new DriverManager().SetUpDriver(new ChromeConfig(), VersionResolveStrategy.MatchingBrowser);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多