public static void Click(this IWebDriver driver, Func <IWebElement> findAction, int timeoutSec = 10) { int c = 0; StaleElementReferenceException exc = null; do { try { exc = null; var el = findAction.Invoke(); el.Click(); } catch (StaleElementReferenceException e) { Thread.Sleep(500); exc = e; } }while (exc != null && c++ < timeoutSec * 2); if (exc != null) { throw exc; } }
public T ProtegerDeStaleReference <T>(Func <T> func, int vezes = 5) { var i = 0; StaleElementReferenceException exception = null; while (i++ < vezes) { try { return(func()); } catch (StaleElementReferenceException ex) { exception = ex; Thread.Sleep(200); } } if (exception == null) { throw new ApplicationException(string.Format("Tentou-se obter o valor {0} vezes, sem sucesso.", vezes)); } throw new ApplicationException("Não conseguiu obter o valor, houve StaleElementReferenceException.", exception); }
/// <summary> /// Try to execute function (usually involving getting some element), and /// if a <see cref="StaleElementReferenceException"/> occurs, retries up /// to the specified number of times. /// </summary> public virtual T ProtectFromStaleReference <T>(Func <T> func, int times = 5) { var i = 0; StaleElementReferenceException exception = null; while (i++ < times) { try { return(func()); } catch (StaleElementReferenceException ex) { exception = ex; Thread.Sleep(200); } } if (exception == null) { throw new ApplicationException($"Tried to get value {times} times, no success."); } throw new ApplicationException("Couldn't get value, got a StaleElementReferenceException.", exception); }
private static IWebElement ExecuteWithoutImplicitTimeout(IWebDriver driver, Func <IWebDriver, IWebElement> action) { if (action == null) { return(null); } StaleElementReferenceException exc = null; int counter = 0; do { try { driver.DisableTimeout(); var el = action(driver); return(el); } catch (StaleElementReferenceException e) { exc = e; Thread.Sleep(500); } finally { driver.SetTimeout(); } }while (exc != null && counter++ < 10); if (counter >= 10 && exc != null) { throw exc; } return(null); }