user1893649
user1893649

Reputation: 177

Getting System.ArgumentException when running C# Selenium Test

I'm learning how to write selenium tests in C# and am getting this error when trying to run the test case below. It is failing on: 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')

Code:

[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");
    };
}

Any ideas? Thank you in advance!

Upvotes: 2

Views: 2455

Answers (1)

Stemado
Stemado

Reputation: 629

Check and make sure your ChromeDriver() version matches what is on your local machine.

Below is an XUnit test that will re-create your issue using the WebDriverManager NuGet package where you can manually specify which version of webdriver you want downloaded if it's not available on your machine:

    [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}");
    }

If you choose to use WebDriverManager in your project, you can use automatically set the below to automatically download a chromedriver.exe matching the version of Chrome installed in your machine so you don't have to manually manage them:

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

Upvotes: 1

Related Questions