Wait in Selenium Webdriver:Implicit, Explicit, & Fluent

Jan 19, 2021, 1469 Views, 2

Contents

  • What is wait in Selenium?
  • Explicit Wait in Selenium
  • Implicit Wait in Selenium
  • Fluent Wait in Selenium using C#
  • Difference Between Explicit wait and Implicit Wait
  • 1.What is wait in Selenium?


    Loading DOM is an asynchronous process, meaning that some elements would get loaded first while others later, and does not necessarily depend on sequence. Now, with javascript, we could use the document.ready() condition and then execute any process, but webdriver is more like instructing the browser to do a certain action without necessarily that the element has executed completely. And then we would get false results are driver throws the error when an element hasn’t found which actually could have been that element has not loaded completely. This makes sense for selenium webdriver creators to add wait conditions. Now, these waits are also of three types:

    1. Explicit Wait

    2. Implicit Wait

    3. Fluent Wait


    Wait in Selenium Webdriver:Implicit, Explicit, & Fluent

    2.Explicit Wait in Selenium


    The good coding practice guide also says to just use Explicit wait as it sets the maximum time interval for the action that needs to be performed as till that time the driver would keep finding the element or perform actions that require syncing between DOM, browser, and selenium webdriver. But once that time interval has crossed the code would throw an exception if the element is not found. Few things to specifically note here is that there is a class in Selenium called ExcpectedConditions to tell the driver to wait for certain events like the text is not visible, the link is not clickable, etc. Also in C#, you need to install a separate NuGet package SeleniumExtras WaitHelpers

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using WebApplication1.Models;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Remote;
    using OpenQA.Selenium.Support.UI;

    namespace WebApplication1.Controllers
    {
        public class HomeController : Controller
        {
            public IActionResult Index()
            {

                ChromeOptions options = new ChromeOptions();
                options.BinaryLocation = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";


                DesiredCapabilities capabilities = new DesiredCapabilities();
                capabilities.SetCapability(ChromeOptions.Capability, options);
                ChromeDriver driver = new ChromeDriver("C:\\WebDriver\\bin ", options);
                driver.Navigate().GoToUrl(@"https://engineerdiaries.com/");

                // Initialize and wait till element(link) became clickable - timeout in 10 seconds
                var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 10));
    //since there is no such element with class "details1-card" would raise exception after 10sec
                IWebElement SearchResult = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.ClassName("details1-card")));
                return View();

            }
        }
    }

    3.Implicit Wait in Selenium


    Implicit Wait is pretty Unlike Explicit as here, you have to directly apply the wait condition at the driver level and not the element level. And there is also no Expected Conditions feature plus it will add the timeout to the entire DOM so that DOM elements could sync at once for the driver. Explicit wait hence is much more feasible than the implicit wait as it at the elemental level. Also, this has slowed down the entire system and it is not recommended by the Selenium official documentation to mix implicit and explicit wait too. Below is an example of implicit wait

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using WebApplication1.Models;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Remote;
    using OpenQA.Selenium.Support.UI;

    namespace WebApplication1.Controllers
    {
        public class HomeController : Controller
        {
            public IActionResult Index()
            {

                ChromeOptions options = new ChromeOptions();
                options.BinaryLocation = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";


                DesiredCapabilities capabilities = new DesiredCapabilities();
                capabilities.SetCapability(ChromeOptions.Capability, options);
                ChromeDriver driver = new ChromeDriver("C:\\WebDriver\\bin ", options);
      driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
                driver.Navigate().GoToUrl(@"https://engineerdiaries.com/");
    IWebElement dynamicElement = driver.FindElement(By.Id("img"));     
                return View();

            }
        }
    }

    4.Fluent Wait in Selenium using C#


    Fluent Wait is similar to Explicit wait only the additional feature to even tell the driver as after how many intervals the action needs to be performed under the max specified time limit. Also ignoring conditions could be used for the exception thrown by the events such as if the element is not found itself. Because driver at times wont be waiting for the max time and throw exceptions for few things under ExpectedCondition and that would just deceive the purpose of the wait. The below example uses fluent wait for polling interval of 5 secs that is repeat the action after every 5 sec and maximum time interval is 30 secs.

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using WebApplication1.Models; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Remote; using OpenQA.Selenium.Support.UI; namespace WebApplication1.Controllers { public class HomeController : Controller { public IActionResult Index() { ChromeOptions options = new ChromeOptions(); options.BinaryLocation = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"; DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.SetCapability(ChromeOptions.Capability, options); ChromeDriver driver = new ChromeDriver("C:\\WebDriver\\bin v", options); //driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10); driver.Navigate().GoToUrl(@"https://engineerdiaries.com/"); //would run the code 6 times ie. after every 5 sec to test WebDriverWait wait = new WebDriverWait(driver, timeout: TimeSpan.FromSeconds(30)) { PollingInterval = TimeSpan.FromSeconds(5), }; wait.IgnoreExceptionTypes(typeof(NoSuchElementException)); var foo = wait.Until(drv => drv.FindElement(By.Id("foo"))); return View(); } } }

    5.Difference Between Explicit wait and Implicit Wait

    As stated Explicit wait is for the element level whereas Implicit wait is for the driver level. Also, ExpectedCondition isnt a thing as the implicit wait. SO, use implicit wait to sync DOM, browser, and driver by choosing a default time for the elements state to change to ready. Explained more below here in the image.

    Wait in Selenium Webdriver:Implicit, Explicit, & Fluent

    Comments Section:

    Admin

    Feb 19, 2021

    Glad it helped. Thank you for the feedback:)

    Manoj Jagdale

    Feb 18, 2021

    Very good explanation it clears all concepts, thanks