I was looking for alternatives, and I decided to use the following versions. Everyone uses an explicit wait with a given timeout and is based on the properties of the element in the first case and on the durability of the element in the second case.
The first choice will check the properties of the element until a timeout is reached. I came to the following properties, confirming that it is available on the page:
Existence . Waiting to verify that the element is present in the DOM of the page. This does not necessarily mean that the item is visible.
//this will not wait for page to load Assert.True(Driver.FindElement(By elementLocator).Enabled) //this will search for the element until a timeout is reached public static IWebElement WaitUntilElementExists(By elementLocator, int timeout = 10) { try { var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout)); return wait.Until(ExpectedConditions.ElementExists(elementLocator)); } catch (NoSuchElementException) { Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page."); throw; } }
Visibility Waiting to verify that the element is present in the DOM of the page and visible. Visibility means that the item is not only displayed, but also has a height and width greater than 0.
//this will not wait for page to load Assert.True(Driver.FindElement(By elementLocator).Displayed) //this will search for the element until a timeout is reached public static IWebElement WaitUntilElementVisible(By elementLocator, int timeout = 10) { try { var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout)); return wait.Until(ExpectedConditions.ElementIsVisible(elementLocator)); } catch (NoSuchElementException) { Console.WriteLine("Element with locator: '" + elementLocator + "' was not found."); throw; } }
Clickable . The pending item check is visible and turned on so that you can click it.
//this will not wait for page to load //both properties need to be true in order for element to be clickable Assert.True(Driver.FindElement(By elementLocator).Enabled) Assert.True(Driver.FindElement(By elementLocator).Displayed) //this will search for the element until a timeout is reached public static IWebElement WaitUntilElementClickable(By elementLocator, int timeout = 10) { try { var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout)); return wait.Until(ExpectedConditions.ElementToBeClickable(elementLocator)); } catch (NoSuchElementException) { Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page."); throw; } }
The second choice applies when a trigger object, such as a menu item, is no longer attached to the DOM after it is clicked. This usually happens when a click action on an element causes a redirect to another page. In this case, it is useful to check StalenessOf (element) , where the element is the element that was clicked to cause a redirect to a new page.
public static void ClickAndWaitForPageToLoad(By elementLocator, int timeout = 10) { try { var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(timeout)); var element = Driver.FindElement(elementLocator); element.Click(); wait.Until(ExpectedConditions.StalenessOf(element)); } catch (NoSuchElementException) { Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page."); throw; } }