0

While trying to run the following FindElements method on a page:

var match = Driver.Instance.FindElements(By.LinkText("Click here"));

I'm receiving the error:

An exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.dll but was not handled in user code.
OpenQA.Selenium.WebDriverException was unhandled by user code
HResult=-2146233088
Message=The HTTP request to the remote WebDriver server for URL http://localhost:7057/hub/session/a90c4828-3fb3-46d1-923d-8c5cbb65c4fe/elements timed out after 60 seconds.
Source=WebDriver

The link text 'Click here' doesn't actually exist in the page, so I'm not expecting FindElements(By) to actually find anything (I'm using it later on in an If statement). The method is timing out causing the above exception.

From my understanding though, if FindElements times out, and doesn't actually find anything, it should return 0 elements. Not just timeout and throw an exception.

Has anyone else come across this or have any ideas what might be causing it?

xander
  • 53
  • 1
  • 8
  • xander, as far as i know, findElements return collection of elements which meets the criteria. It can be Zero or more elements. I am hoping the problem is assignment of collection to "var match". Can u have a statement like List eles=your findements and give a try – Uday Dec 17 '14 at 13:41
  • What version of Selenium, what browser and what version of that browser? – Arran Dec 17 '14 at 15:36

1 Answers1

2

FindElements() would throw a WebDriverExepction because how I had created the driver.

I had created the Driver in it's own class:

public class Driver
{
    public static IWebDriver Instance { get; private set; }

    public static void Initialise()
    {
        Instance = new FirefoxDriver();
        Instance.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
    }
}

This meant at the start of a test case I would call Driver.Initialise() and then use Driver like so:

Driver.Instance.FindElements(By.Id("ABC"));

For some reason (I still don't know the real answer), the Driver class not being static and calling the FindElements method would return the WebDriverException instead of a List of 0 elements.

Simply changing the class to:

public static class Driver
{
    public static IWebDriver Instance { get; private set; }

    public static void Initialise()
    {
        Instance = new FirefoxDriver();
        Instance.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
    }
}

Fixed the problem.

Niels R.
  • 7,260
  • 5
  • 32
  • 44
xander
  • 53
  • 1
  • 8