/// <summary> /// Waits for an element to be missing. /// </summary> /// <param name="by">Search mechanism</param> /// <param name="timeout">Optional - timeout in milliseconds</param> public void WaitNotElement(By by, int timeout = -1) { if (by.Strategy == Strategy.Any) { WaitAnyElementNotPresent(by, timeout); } else { WaitElementNotPresent(by.Strategy, (string)by.Value, timeout); } }
/// <summary> /// Find the first WebElement using the given method. /// </summary> /// <param name="by">Methode</param> /// <param name="timeout">Optional timeout in milliseconds</param> /// <param name="raise">Optional - Raise an exception after the timeout when true</param> /// <returns><see cref="WebElement" /> or null</returns> /// <example> /// <code lang="vbs"> /// Set elts = driver.FindElement(By.Any(By.Name("name"), By.Id("id"))) /// </code> /// </example> public WebElement FindElement(By by, int timeout = -1, bool raise = true) { try { if (by.Strategy == Strategy.Any) return FindAnyElement(by, timeout); return FindFirstElement(by.Strategy, (string)by.Value, timeout); } catch (Errors.NoSuchElementError) { if (raise) throw new Errors.NoSuchElementError(by); return null; } catch (Errors.ElementNotVisibleError) { if (raise) throw new Errors.ElementNotVisibleError(by); return null; } }
/// <summary> /// Returns a web element matching the given method and value /// </summary> private void WaitAnyElementNotPresent(By byAny, int timeout) { RemoteSession session = this.session; string uri = this.uri + "/element"; DateTime endTime = session.GetEndTime(timeout); foreach (By by in (By[])byAny.Value) { if (by == null) break; try { string method = By.FormatStrategy(by.Strategy); string value = by.Value.ToString(); session.Send(RequestMethod.POST, uri, "using", method, "value", value); while (true) { if (DateTime.UtcNow > endTime) throw new Errors.ElementPresentError(byAny); SysWaiter.Wait(); session.SendAgain(); } } catch (Errors.NoSuchElementError) { } } }
private WebElements FindAnyElements(By byAny, int minimum, int timeout) { RemoteSession session = this.session; string uri = this.uri + "/elements"; WebElements webelements = new WebElements(); DateTime endTime = session.GetEndTime(timeout); while (true) { foreach (By by in (By[])byAny.Value) { if (by == null) break; var method = By.FormatStrategy(by.Strategy); var value = (string)by.Value; List elements = (List)session.Send(RequestMethod.POST, uri, "using", method, "value", value); webelements.Add(session, elements); } if (webelements.Count >= minimum) return webelements; if (DateTime.UtcNow > endTime) throw new Errors.NoSuchElementError(byAny); SysWaiter.Wait(); } }
private WebElement FindAnyElement(By byAny, int timeout) { RemoteSession session = this.session; string relativeUri = this.uri + "/element"; Dictionary element; DateTime endTime = session.GetEndTime(timeout); while (true) { foreach (By by in (By[])byAny.Value) { if (by == null) break; try { string method = By.FormatStrategy(by.Strategy); string value = by.Value.ToString(); element = (Dictionary)session.Send(RequestMethod.POST, relativeUri, "using", method, "value", value); return new WebElement(session, element); } catch (Errors.NoSuchElementError) { } } if (DateTime.UtcNow > endTime) throw new Errors.NoSuchElementError(byAny); SysWaiter.Wait(); } }
/// <summary> /// "Verifies that the specified element is somewhere on the page." /// </summary> /// <param name="by">An element loctor. string or By object</param> /// <param name="timeout">Optional timeout in milliseconds</param> /// <returns>true if the element is present, false otherwise</returns> public bool IsElementPresent(By by, int timeout = 0) { return FindElement(by, timeout, false) != null; }
/// <summary> /// Find all elements within the current context using the given mechanism. /// </summary> /// <param name="by">The locating mechanism to use</param> /// <param name="minimum">Minimum number of elements to wait for</param> /// <param name="timeout">Optional timeout in milliseconds</param> /// <returns>A list of all WebElements, or an empty list if nothing matches</returns> /// <example> /// <code lang="vbs"> /// Set elts = driver.FindElements(By.Any(By.Name("name"), By.Id("id"))) /// </code> /// </example> public WebElements FindElements(By by, int minimum = 0, int timeout = 0) { if (by.Strategy == Strategy.Any) return this.FindAnyElements(by, minimum, timeout); return this.FindElementsBy(by.Strategy, (string)by.Value, minimum, timeout); }
/// <summary> /// Search using multiple mechanisms /// </summary> /// <param name="by1">Mechanism 1</param> /// <param name="by2">Mechanism 2</param> /// <param name="by3">Optional - Mechanism 3</param> /// <param name="by4">Optional - Mechanism 4</param> /// <param name="by5">Optional - Mechanism 5</param> /// <param name="by6">Optional - Mechanism 6</param> /// <returns>By object</returns> By _By.Any(By by1, By by2, By by3, By by4, By by5, By by6) { return new By(by1, by2, by3, by4, by5, by6); }
/// <summary> /// Search using multiple mechanisms /// </summary> /// <param name="by1">Mechanism 1</param> /// <param name="by2">Mechanism 2</param> /// <param name="by3">Optional - Mechanism 3</param> /// <param name="by4">Optional - Mechanism 4</param> /// <param name="by5">Optional - Mechanism 5</param> /// <param name="by6">Optional - Mechanism 6</param> /// <returns>By object</returns> public static By Any(By by1, By by2, By by3 = null, By by4 = null, By by5 = null, By by6 = null) { return new By(by1, by2, by3, by4, by5, by6); }