Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium - C# - Webdriver - Unable to find element

Using selenium in C# I am trying to open a browser, navigate to Google and find the text search field.

I try the below

IWebDriver driver = new InternetExplorerDriver(@"C:\");

driver.Navigate().GoToUrl("www.google.com");

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));

IWebElement password = driver.FindElement(By.Id("gbqfq"));

but get the following error -

Unable to find element with id == gbqfq

like image 501
user3535954 Avatar asked May 02 '26 22:05

user3535954


1 Answers

This looks like a copy of this question that has already been answered.

I can show you what I've done, which seems to work well for me:

public static IWebElement WaitForElementToAppear(IWebDriver driver, int waitTime, By waitingElement)
{
        IWebElement wait = new WebDriverWait(driver, TimeSpan.FromSeconds(waitTime)).Until(ExpectedConditions.ElementExists(waitingElement));
        return wait;
}

This should wait waitTime amount of time until either the element is found or not. I've run into a lot of issues with dynamic pages not loading the elements I need right away and the WebDriver trying to find the elements faster than the page can load them, and this is my solution to it. Hope it helps!

like image 83
sparkyShorts Avatar answered May 04 '26 10:05

sparkyShorts



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!