0
votes

I am using selenium WebDriverWait object to dynamically control the timeout for object detection in my code. But this object is timing out before the timespan specified in WebDriverWait object is spent. For ex. I have object initialized with 10 seconds timeout time span but object is timing out in around 2 seconds.

Here is my code

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
try
{
  wait.Until(drv => (drv.FindElement(By.XPath(".//*[@class='TopTitle']"))));   //drv.FindElement(By.XPath(".//*[@class='TopTitle']")) throws exception
}
catch
{
  //exception handlers
}

I am running it in debug mode and i can clearly see drv.FindElement(By.XPath(".//*[@class='TopTitle']")) within 2-3 seconds

Exception and stack trace:

Unable to locate element: {"method":"xpath","selector":".//*[@class='TopTitle']"}
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByXPath(String xpath)
at OpenQA.Selenium.By.<>c_DisplayClasse.b_c(ISearchContext context)
at OpenQA.Selenium.By.FindElement(ISearchContext context)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
at VS_UnitTest.Navigation.<>c_DisplayClass2.b_0(IWebDriver drv) in c:\Git\WebadminTests\WebAdminTests\Navigation.cs:line 511
at OpenQA.Selenium.Support.UI.DefaultWait.Until[TResult](Func condition)

1
If you run it locally you are going to see the exceptions regardless - Visual Studio is set, by default, to catch & show you most exceptions. - Arran
This didn't throw any exception before today. And i am still testing the same UI i was testing for months. Now it is throwing exception even on home page, where element is clearly present. But drv.FindElement(By.XPath(".//*[@class='TopTitle']"))) throws Unable to find element exception - Rahul Lodha
Then you've got an entirely different issue not related to the WebDriverWait at all. What's changed? What browser is this in? What version of Selenium? Have you upgraded that recently? If not, then you can be sure it isn't Selenium at fault if your backing library hasn't been changed. Show us the full error & stack trace. - Arran
Absolutely nothing has changed. used to work till now. - Rahul Lodha
So what browser is this in? Where's the error and stack trace? - Arran

1 Answers

0
votes

WebDriverWait only handles certain kinds of exceptions. You need to handle the other kinds yourself by using the FluentWait.ignoring feature (WebDriverWait is an extension of FluentWait). If you look at the source code, it appears that WebDriverWait only ignores NotFoundException.class on its own. To ignore other exceptions, you need to add a catch class (or a generic catch clause). NOTE: In the case of TimeoutException... do not use that in the ignoring clause and instead wrap WebDriverWait in a try-catch and catch the TimeoutException outside of the wait.