Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Inspired by Loudenvier's solution, here's an extension method that works for all ISearchContext objects, not just IWebDriver, which is a specialization of the former. This method also supports waiting until the element is displayed.</p> <pre><code>static class WebDriverExtensions { /// &lt;summary&gt; /// Find an element, waiting until a timeout is reached if necessary. /// &lt;/summary&gt; /// &lt;param name="context"&gt;The search context.&lt;/param&gt; /// &lt;param name="by"&gt;Method to find elements.&lt;/param&gt; /// &lt;param name="timeout"&gt;How many seconds to wait.&lt;/param&gt; /// &lt;param name="displayed"&gt;Require the element to be displayed?&lt;/param&gt; /// &lt;returns&gt;The found element.&lt;/returns&gt; public static IWebElement FindElement(this ISearchContext context, By by, uint timeout, bool displayed=false) { var wait = new DefaultWait&lt;ISearchContext&gt;(context); wait.Timeout = TimeSpan.FromSeconds(timeout); wait.IgnoreExceptionTypes(typeof(NoSuchElementException)); return wait.Until(ctx =&gt; { var elem = ctx.FindElement(by); if (displayed &amp;&amp; !elem.Displayed) return null; return elem; }); } } </code></pre> <h3>Example usage:</h3> <pre><code>var driver = new FirefoxDriver(); driver.Navigate().GoToUrl("http://localhost"); var main = driver.FindElement(By.Id("main")); var btn = main.FindElement(By.Id("button")); btn.Click(); var dialog = main.FindElement(By.Id("dialog"), 5, displayed: true); Assert.AreEqual("My Dialog", dialog.Text); driver.Close(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload