We all perform some operation in UI of a page if the control exist or displayed. This can be done using IWebElement.Displayed property in Selenium.
But the problem is, if the control does not appear before we try to check if it exist in the page, it will throw us a StaleElementReferenceException or NoSuchElementException, which is expected, since the page might have loaded slowly were as the Selenium is fast enough to execute the operation.
Hence try to always write the IWebElement.Displayed
in try catch block so that you don’t end up having exception.
And you can wait for the control using Explicit wait as shown below.
public static void WaitForControlExist(IWebDriver driver,IWebElement element)
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(40));
catch (NoSuchElementException e)
//Write No element exception
The method above will wait explicitly for the control exist for 40 seconds, since we have handled the exception for NoSuchElementException
the StaleElementRefereceException does not appears
This way we can wait for the control safely without ending up with exceptions using IWebElement.Displayed property!!!