3

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

user3535954
  • 31
  • 1
  • 1
  • 3
  • 1
    What version of Internet Explorer are you using? There are known issues with IE 11 support with Selenium Web Driver. – Martin Costello Apr 15 '14 at 12:48
  • 4
    Also, my honest suggestion would be to use **any other webpage other than Google**. It is unfortunate most people choose Google as their "starting application" for Selenium work but it is, by far, one of the most complex sites you will come across (i.e the markup is hell, and minimized massively) - you will encounter issues working with Google's site's that you wouldn't with others. Save yourself the hassle to begin with! – Arran Apr 15 '14 at 14:12
  • You've probably already checked this--but that ID isn't dynamically generated, right? If so, then the ID won't be a reliable way to find the element... – autoKarma Apr 16 '14 at 10:40
  • 1
    possible duplicate of [Selenium c# Webdriver: Wait Until Element is Present](http://stackoverflow.com/questions/6992993/selenium-c-sharp-webdriver-wait-until-element-is-present) – Louis Nov 07 '14 at 16:13

2 Answers2

2

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!

Community
  • 1
  • 1
sparkyShorts
  • 630
  • 9
  • 28
1

You can try using a spin wait

int timeout =0;
while (driver.FindElements(By.id("gbqfq")).Count == 0 && timeout <500){
  Thread.sleep(1);
  timeout++;

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

this should help make sure that the element has actually had time to appear.

also note, the "gbqfq" id is kinda a smell. I might try something more meaningful to match on than that id.

Willcurles
  • 71
  • 1
  • 3
  • 2
    This is just a re-implementation of the `WebDriverWait`. Also `Count` should be replaced with `Any`. – Arran Apr 16 '14 at 08:19