Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Wait Types: An Important Aspect in the Selenium Automation Process

DZone's Guide to

Wait Types: An Important Aspect in the Selenium Automation Process

In this quick tutorial, we'll go over Implicit, Explicit, and Fluent wait types, and how to go about using them in an automated Selenium environment.

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

In this blog, I would be discussing the important aspects of different wait types in Selenium's automation process. I have seen that there is a huge confusion in understanding different wait types and its significance when it comes to Selenium automation. So this blog would exclusively explain what the different wait types are.

There are basically three wait types in Selenium automation:

  1. Implicit Wait Type
  2. Explicit Wait Type
  3. Fluent Wait Type

Implicit Wait Type

Selenium WebDriver has borrowed the idea of ‘Implicit Wait’ type from Watir. This basically tells Selenium to wait for a certain amount of time before throwing an exception if cannot find the element of the page. We should note that the implicit wait will be in place for the entire time the browser is open. This means that any search for elements on the page by the webdriver should use the time designated for the implicit wait. If we do not provide Selenium with this wait, then Selenium would start throwing ElementNotFound exceptions because the time taken for Selenium to interact with the corresponding element in the browser was not given. This is applicable for all the web elements in the browser for the entire browser session. For example:

from selenium import webdriver

class BrowserInteractions():
    def test(self):
        driver = webdriver.Firefox()
        driver.get('https://google.com')
        driver.implicitly_wait(10)

Explicit Wait Type

Explicit wait is more extendable, in so far as you can set it up to wait for any specific condition. Usually, we use theExpectedCondition object to specify the condition to provide the corresponding explicit wait. For example:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class BrowserInteractions():
    def test(self):
        driver = webdriver.Chrome()
        driver.get('http://www.quora.com/Kevin-Rose')
        element = WebDriverWait(driver, 2).until(
            EC.presence_of_element_located((By.
            PARTIAL_LINK_TEXT, "Followers")))
        element.click()

Fluent Wait Type

Fluent wait is a part of the Explicit Wait type. Fluent wait instances define the maximum amount of time to wait for a condition, as well the frequency with which to check the condition. For example:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import *
import time

class ExplicitWait():
    def test(self):
        baseURL = "http://www.cleartrip.com/"
        bin_path = "/usr/local/bin/chromedriver"
        driver = webdriver.Chrome(executable_path=bin_path)
        driver.get(baseURL)
        driver.implicitly_wait(.5)
        driver.maximize_window()
        input_from = driver.find_element(By.ID,"FromTag")
        input_from.send_keys("blr")
        input_to = driver.find_element(By.ID,"ToTag")
        input_to.send_keys("ccu")
        input_depart_date = driver.find_element(By.ID,"DepartDate")
        input_depart_date.send_keys("Sun, 4 Jun, 2017")
        input_depart_date.send_keys(Keys.ENTER)

        input_searchBtn = driver.find_element(By.ID,"SearchBtn")
        input_searchBtn.click()
        wait = WebDriverWait(driver, 10, poll_frequency=1,
               ignored_exceptions=[NoSuchElementException,
                                   ElementNotVisibleException,
                                   ElementNotSelectableException])
        element = wait.until(EC.
                  element_to_be_clickable((By.ID,"1_1_MULTI")))
        element.click()
        time.sleep(2)
        driver.quit()

ew = ExplicitWait()
ew.test()

This is a very important concept when we are performing automation using Selenium, and I hope I was able to clear the difference between the three wait types.

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:
automation ,selenium ,webdriver ,web dev

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}