2. In our framework, wherever we are handling StaleElementReferenceException , we use an additional try...catch(WebDriverException) and add a check for IsActuallyStaleElementException e.g.:
public class CommonExpectedConditions
{
/// <summary>
/// An expectation for checking that an element is either invisible or no longer present on the DOM.
/// </summary>
/// <param name="element">The locator used to find the element.</param>
/// <returns><see langword="true"/> if the element is not displayed; otherwise, <see langword="false"/>.</returns>
public static Func<IWebDriver, bool> InvisibilityOfElement(IWebElement element)
{
return (driver) =>
{
try
{
return !element.Displayed;
}
catch (NoSuchElementException)
{
// Returns true because the element is not present in DOM. The
// try block checks if the element is present but is invisible.
return true;
}
catch (StaleElementReferenceException)
{
// Returns true because stale element reference implies that element
// is no longer visible.
return true;
}
catch (WebDriverException ex)
{
if (ex.IsActuallyStaleElementException())
return true;
throw;
}
};
}
}