Search code examples
c#.netseleniumnosuchelementexception

.NET Selenium NoSuchElementException; WebDriverWait.Until() doesn't work


We have a .NET 4.5, MVC, C# project. We're using Selenium for UI tests, and the tests keep intermittently failing on lines that we have wait.Until(). One such example is:

NoSuchElementException was unhandled by user code
An exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll but was not handled in user code
Additional information: Unable to locate element: {"method":"css selector","selector":"#assessment-472 .status-PushedToSEAS"}

It's thrown right here:

Thread.Sleep(600);
wait.Until(drv => drv.FindElement(By.CssSelector("#" + assessmentQueueId + " .status-PushedToSEAS")));

I can see that the browser opens, I see it get to that point, and I can inspect element to see that the element exists. Its id is exactly correct.

We have this problem A LOT and so far the solution has been to throw Thread.Sleep(600) (or some similar time) in front of it. The whole point of wait.Until() is to not have to do that, and this is making our test suites get very long. Also, as you can see in the example above, sometimes we have the problem even after putting a Thread.Sleep() in front of it and we have to extend the time.

Why is .NET Selenium's WebDriver.Until() not working, and is there a different way to do the same thing without just waiting a set period of time? Again, the problem is intermittent, meaning that it only happens sometimes, and it could happen at any number of wait.Until() statements, not just the one shown!

Edit:

This is a class variable. private WebDriverWait wait;

It is instantiated like this:

this.wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

Solution

  • I decided to just not use WebDriverWait.Until(), and use implicit waits on the main driver instead:

    driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
    

    I give full credit to this answer on a different question for giving me the idea.