Learn Selenium Wait Commands: Implicit, Explicit and Fluent Wait

In this post, you will learn about different waits in Selenium. Waits are required to tell Selenium to wait for a specific amount of elements on the web page before throwing "No Such Element Exception". There are three types of waits in Selenium WebDriver, out of which the first two are the most popular.
  1. Implicit Wait
  2. Explicit Wait
  3. Fluent Wait

1. Selenium WebDriver Wait Commands

1.1. Implicit Wait in Selenium WebDriver

The implicit wait is used to set a default wait time (say 30 secs) in your automation script to wait for an element on the page.

Key Points

  • The implicit wait should be declared in your setup method (at the beginning lines of your code).
  • The default wait time for Implicit wait is 0 secs.
  • Once the implicit wait is set, it is applicable to the whole automation script.
  • In simple words, you can also say that implicit wait is the maximum time set between two steps/commands of the automation script.
  • Webdriver throws a "No Such Element Exception" if an element is not found in the specified time.

Syntax:

// Implicit wait - Set wait of 10 seconds
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

1.2. Explicit Wait in Selenium WebDriver

An explicit wait can be called a conditional wait. It tells the WebDriver to wait for certain conditions before the maximum time is exceeded.

Key Points

  • The explicit wait is an intelligent wait, as it waits for certain conditions.
  • It applies to only a single element and not to the whole script.
  • It provides a better approach to handling dynamic Ajax elements.
  • The explicit wait is the best way to replace Thread.sleep() (as static waits are not recommended in automation).
  • WebDriver throws "ElementNotVisibleException" if explicit wait fails.

Syntax:

// Explicit Wait
 WebDriverWait wait = new WebDriverWait(driver, 10); 
 wait.until(ExpectedConditions.alertIsPresent());

All Expected Conditions that can be used in Explicit Wait:

    1. alertIsPresent()
    2. elementSelectionStateToBe()
    3. elementToBeClickable()
    4. elementToBeSelected()
    5. frameToBeAvaliableAndSwitchToIt()
    6. invisibilityOfTheElementLocated()
    7. invisibilityOfElementWithText()
    8. presenceOfAllElementsLocatedBy()
    9. presenceOfElementLocated()
    10. textToBePresentInElement()
    11. textToBePresentInElementLocated()
    12. textToBePresentInElementValue()
    13. titleIs()
    14. titleContains()
    15. visibilityOf()
    16. visibilityOfAllElements()
    17. visibilityOfAllElementsLocatedBy()
    18. visibilityOfElementLocated()

    1.3. Fluent Wait in Selenium WebDriver

    The fluent wait is a kind of conditional wait but with frequency. It means the fluent wait is used to wait for a condition with a regular frequency/time interval to be checked before throwing "ElementNotVisibleException".

    Example

    There might be a scenario where element X might load within 30 secs. In this case, we can set 5 secs of frequency, so it will check after every 5 secs for the element X. So, ideally, it will check 6 times before throwing "ElementNotVisibleException". If element X loads within 30 secs, the program flow would move to the next step of the automation script.

    Syntax:

    // Fluent Wait
     Wait wait = new FluentWait(WebDriver reference)
       .withTimeout(timeout, SECONDS)
       .pollingEvery(timeout, SECONDS)
       .ignoring(Exception.class);
    
     WebElement foo = wait.until(new Function() {
      public WebElement applyy(WebDriver driver) {
       return driver.findElement(By.id("foo"));
      }
     });

    Actions Class in Selenium  << Previous     ||     Next >>  Take Screenshot in Selenium


    Author
    Vaneesh Behl
    Passionately writing and working in Tech Space for more than a decade.

    Follow Techlistic

    YouTube Channel | Facebook Page | Telegram Channel | Quora Space
    Feel free to ask queries or share your thoughts in comments or email us.

    Comments

    Popular posts from this blog

    Mastering Selenium Practice: Automating Web Tables with Demo Examples

    17 Best Demo Websites for Automation Testing Practice

    Automate Amazon like E-Commerce Website with Selenium WebDriver

    10 Best Selenium Practice Exercises for Automation Practice

    How to Automate Google Search with Selenium WebDriver

    Top 10 Highly Paid Indian CEOs in the USA

    How I learned Selenium WebDriver in just 4 weeks

    Python Behave Tutorial: A Comprehensive Guide to Behavior-Driven Development (BDD)

    Mastering Selenium WebDriver: 25+ Essential Commands for Effective Web Testing

    Find Broken Links on Webpage with Selenium Automation