Reputation: 11
I am new to selenium and web driver.My question is pretty straight forward. Sorry if you find this question of very basic level. I am using Visual studio 2010 ultimate with selenium 2 and using language C# with browser IE 9.I tried to execute the below simple code.
IWebDriver driver = new InternetExplorerDriver(@"D:\IEDriverServer\");
driver.Navigate().GoToUrl("http://www.google.com");
System.Console.WriteLine("Page title is: " + driver.Title);
Console.WriteLine(driver.Title);
Console.WriteLine("Waiting to find element");
IWebElement returnedValue = driver.FindElement(By.Name("q"));
Console.WriteLine("Element Found");
Above code throws the Unable to find the element with name==...
I thought that it might the browser loading issue. Browser doesnt get fully loaded hence causing it to show the error. Then i added the below code after driver.navigate line to make it wait till the browser fully loads.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
wait.Until(ExpectedConditions.TitleIs("Google"));
Strange thing is that it again ignores this webdriverwait line and jumps directly to "wait.until
" line resulting in displaying the element not found error. What should i do. am i missing something here??
Upvotes: 1
Views: 2403
Reputation: 2957
you are almost right in defining the wait ...but in C# there is different way to enable Webdriver wait till the element is visible till page is loaded.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Name("q"));
});
Hope this helps...All the best buddy :)
Upvotes: 2
Reputation: 4621
Change wait.Until(ExpectedConditions.TitleIs("Google"));
to wait.Until(ExpectedConditions.visibilityOfElementLocated(By.name("q")))
There may be the case that the title of the page had been changed to google but the complete page was not loaded completely
Upvotes: 0
Reputation: 106
Try setting an implicit wait with driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
.
According to the Selenium documentation this is by default 0 - that is, if the element is not found immediately, Selenium throws the "Unable to find the element with..." error.
Upvotes: 1