Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic example of Selenium in C# error ElementDictionary (Parameter 'The specified dictionary does not contain an element reference')

I requested ChatGPT to generate a C# code using Selenium to access a webpage and retrieve an element's value. After attempting this with LinkedIn and Medium pages and encountering errors, I simplified the task by using the simplest page I know: www.pudim.com.br. I tried to obtain the email value but faced errors again. Consequently, I removed all code and attempted to retrieve just the BODY element, which also resulted in an error.

Code:

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System.Threading;

class Program
{
    static void Main()
    {
        ChromeOptions options = new ChromeOptions();
        options.AddArgument("--headless"); // Roda sem abrir o navegador
        options.AddArgument("--disable-gpu");
        options.AddArgument("--no-sandbox");
        options.AddArgument("--disable-dev-shm-usage");

        using (IWebDriver driver = new ChromeDriver(options))
        {
            try
            {
                string url = "http://www.pudim.com.br";
                driver.Navigate().GoToUrl(url);

                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
                wait.Until(d => d.FindElement(By.TagName("body")));

                // All code in the next line was turned off to just test the access to BODY
                // string pageSource = driver.PageSource;
                // Console.WriteLine("Page Source: ");
                // Console.WriteLine(pageSource);

                // try
                // {
                //     IWebElement emailDiv = driver.FindElement(By.ClassName("email"));

                //     IWebElement emailLink = emailDiv.FindElement(By.TagName("a"));
                //     string emailTexto = emailLink.Text;

                //     Console.WriteLine($"E-mail: {emailTexto}");
                // }
                // catch (NoSuchElementException)
                // {
                //     Console.WriteLine("No classe 'email' found.");
                // }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error: {e.Message}");
            }
            finally
            {
                driver.Quit();
            }
        }
    }
}

The output on VS Code is:

Starting ChromeDriver 73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72) on port 50161
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
Error: elementDictionary (Parameter 'The specified dictionary does not contain an element reference')

Any tips on how to fix it? ChatGPT's suggestions lead to the same error.

like image 559
Jean J. Michel Avatar asked Dec 09 '25 16:12

Jean J. Michel


2 Answers

This is a weird error. I've learned that weird errors in Selenium usually indicate a mismatch in the web driver version and web browser version.

I noticed in the command line output that you are using ChromeDriver version 73.x. According to Google, the latest stable ChromeDriver version as of writing this answer is 114.x.

Google Chrome likes to auto-update itself. I would start by double-checking the Google Chrome web browser version to make sure it is compatible with the web driver. My intuition says you need only update ChromeDriver, especially since Bouke commented that the same code worked just fine.

like image 155
Greg Burghardt Avatar answered Dec 11 '25 05:12

Greg Burghardt


The issue was caused by “Visual Code”.

When I first opened the project, VS Code prompted about trusting the author.

I clicked “Yes, I trust the authors” but did not check “Trust the authors of all files in the parent folder ‘projects’”.

This caused the errors.

enter image description here

Thanks!

like image 41
Jean J. Michel Avatar answered Dec 11 '25 07:12

Jean J. Michel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!