public static IWebElement UniqueElement(this IWebDriver webDriver, Selector selector) { IWebElement element = null; Exception exception = null; int retry = 0; while (element == null && retry < FetchRetries) { var by = ByHelper.Construct(selector); var elements = webDriver.FindElements(by); var displayedElements = elements.Where(e => e.Displayed); if (displayedElements.Count() > 1) { exception = new InvalidSelectorException($"{selector.Name} return more than 1 displayed element"); } element = displayedElements.FirstOrDefault(); System.Threading.Thread.Sleep(RetrySleepInterval); retry++; } if (element == null) { if (exception == null) { throw new InvalidSelectorException($"{selector.Name} no element to return with this selector"); } throw exception; } return(element); }
public static IWebElement UniqueElement(this IWebDriver webDriver, Selector selector) { _log_.Trace($"Get UniqueElement {selector.SelectorType.ToString()} {selector.Text}"); IWebElement element = null; Exception exception = null; int retry = 0; while (element == null && retry < FetchRetries) { var by = ByHelper.Construct(selector); try { var elements = webDriver.FindElements(by); var displayedElements = elements.Where(e => e.Displayed); var webElements = displayedElements.ToList(); if (webElements.Count > 1) { exception = new InvalidSelectorException( $"{selector.Text} return more than 1 displayed element"); } element = webElements.FirstOrDefault(); System.Threading.Thread.Sleep(RetrySleepInterval); _log_.Trace($"Fetching element {selector.SelectorType.ToString()} {selector.Text} - Retry {retry}/{FetchRetries}"); } catch (Exception ex) { _log_.Warn($"{ex.StackTrace} {ex.Message}"); //Silently error } retry++; } if (element == null) { if (exception == null) { throw new InvalidSelectorException($"No element to return with this selector {selector.Text}"); } throw exception; } return(element); }