When it comes to automated testing, there’s not much worse than intermittent failures, especially when they stem from the driver itself. The current version of the C# WebDriver bindings has such a failure, but I worked out a reasonable way to avoid it happening. Basically it involves creating a WebDriver extension method that I use instead of Driver.FindElement, which tries a number of times to find the element, ignoring the exception that is intermittently raised.
I hope you find this useful if you’re consuming WebDriver in C#.
using OpenQA.Selenium; using OpenQA.Selenium.Support.UI; namespace Extensions { public static class WebDriverExtensions { public static SelectElement GetSelectElement(this IWebDriver driver, By by) { return new SelectElement(driver.GetElement(by)); } public static IWebElement GetElement(this IWebDriver driver, By by) { for (int i = 1; i <= 5; i++ ) { try { return driver.FindElement(by); } catch (Exception e) { Console.WriteLine("Exception was raised on locating element: " + e.Message); } } throw new ElementNotVisibleException(by.ToString()); } } }
=====================
Usage:
However, we do get intermittent no response from session during Click, SendKeys, Clear, etc, at times. For these we have taken similar approach — just keep trying and catching WebDriverException until the session responds.
Taken from here : http://watirmelon.com/2011/10/04/c-avoiding-the-webdriverexception-no-response-from-server-for-url/