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
2 Answers
Reset to default 1This 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!
本文标签:
版权声明:本文标题:Basic example of Selenium in C# error ElementDictionary (Parameter 'The specified dictionary does not contain an element 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745203985a2647541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论