2

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!

1
  • I've written my fair share of Selenium tests and the answer isn't obvious to me, but this code, and similar, appears to be what throws that exception. Seems related to a missing element ID. Not sure if this helps but it's something. I personally would debug and step through the Selenium code. Commented Mar 6, 2020 at 0:52

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.