admin管理员组

文章数量:1415684

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.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 = ";;
                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.

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.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.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.

Share Improve this question asked Feb 12 at 20:30 Jean J. MichelJean J. Michel 6112 gold badges8 silver badges14 bronze badges 1
  • 2 Copied the code and run it. No error here. – Bouke Commented Feb 12 at 21:39
Add a comment  | 

2 Answers 2

Reset to default 1

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.

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.

Thanks!

本文标签: