Having serious issues with waits! Selenium WebDriver C#

Hi All,

Iā€™m currently creating my first automation framework and first test case - so far so good, but Iā€™m now running into serious issues trying to get a decent ā€˜waitā€™ implemented.

My research so far is telling me that explicit waits are better practice than implicit wait, and sleeps should be avoided like the plague unless absolutely unavoidable. The test that I am running against the website is intermittently throwing up ā€˜element not foundā€™ or ā€˜element not visibleā€™ exceptions and terminating the test run - if I rerun the test then the previous exception might suddenly pass but then another one will fail.

I can only presume that this behaviour is down to the time the site is taking to load elements, but the thing thatā€™s really confusing me is that even when I wait for an element to be present and then try to interact with it, I may still get an exception sometimes.

For example, Iā€™ll do this check to ensure that this element is available:

new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementExists((By.XPath(//LABEL[@data-toggle='dropdown))));

Then Iā€™ll click on it:

Driver.Instance.FindElement(By.XPath("//LABEL[@data-toggle='dropdown")).Click();

But this will sometimes not work. The element is a dropdown menu and I know itā€™s on the page and should be clickable, so am completely stumped!

Any advice on a good approach to waiting to a: run tests and quickly as possible and b: not have them fall over due to running too quickly would be greatly appreciated :slight_smile:

Thanks,

James

As is often the case with me, two days of searching in vain and about half an hour after this post I seem to have stumbled across a fix!

Turns out that replacing ExpectedConditions.ElementExists with ExpectedConditions.ElementToBeClickable has apparently solved all my issues! I guess that beforehand the check was just ensuring that the element existed and Selenium tried to select it before it was actually clickable!

:sunglasses:

1 Like