Waits in Selenium Webdriver

Posted By :Rohitesh Rawat |30th April 2018
Waits in Selenium WebDriver
 
ImplicitWait: 
      
If elements are not immediately available, an implicit wait tells the WebDriver to poll the DOM for a certain amount of time.
 
The default setting is 0. Once set, the implicit wait is set for the duration of the WebDriver object.
 
This means that we can tell Selenium WebDriver that we would like it to wait for a certain amount of time before throwing an exception that it cannot find the element on the page.       
                     
 
Explicit Wait:
       
Explicit Wait is the custom one.
 
It will be used if we want the execution to wait for some time until some condition is achieved.
 
An explicit wait is what we can define to wait for a certain condition to occur before proceeding further in the code.
 
 
 
 
Below are some of the available expected conditions:
  
• alert_is_present
 
• element_located_selection_state_to_be  
  
• element_located_to_be_selected
  
• element_selection_state_to_be
  
• element_to_be_clickable
  
• element_to_be_selected
  
• frame_to_be_available_and_switch_to_it
  
• invisibility_of_element_located
  
• presence_of_all_elements_located
  
• presence_of_element_located
  
• staleness_of
  
• text_to_be_present_in_element
 
• text_to_be_present_in_element_value
  
• title_contains
  
• title_is
  
• visibility_of
 
• visibility_of_element_located
  
 
  
 
Example of Implicit wait:
 
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

class ImplicitWait():
    def test(self):
        baseUrl = "https://twitter.com/?lang=en"
        driver = webdriver.Firefox()
        driver.maximize_window()
        driver.get(baseUrl)
        driver.implicitly_wait(10)

        TloginLink = driver.find_element(By.XPATH, "xpath address")
        TloginLink.click()

        TemailField = driver.find_element(By.ID, "id input")
        TemailField.send_keys("email")

ff = ImplicitWait()
ff.test()
 
 
Example of Explicit wait:
 
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import *

class ExplicitWait():

    def test(self):
        baseUrl = "http://www.expedia.com"
        driver = webdriver.Firefox()
        driver.implicitly_wait(.5)
        driver.maximize_window()
        driver.get(baseUrl)
        driver.find_element(By.ID, "id input").click()
        driver.find_element(By.ID, "id input").send_keys("enter keys")
        driver.find_element(By.ID, "id input").send_keys("enter keys")
        driver.find_element(By.ID, "id input").send_keys("enter keys")
        returnDate = driver.find_element(By.ID, "id input")
        returnDate.clear()
        returnDate.send_keys("enter keys")
        driver.find_element(By.ID, "id input").click()

        ewait = WebDriverWait(driver, 10, poll_frequency=1,
                             ignored_exceptions=[NoSuchElementException,
                                                 ElementNotVisibleException,
                                                 ElementNotSelectableException])
        elem = wait.until(EC.element_to_be_clickable((By.ID,
                                                         "id input")))
        elem.click()

        time.sleep(2)
        driver.quit()

ff = ExplicitWait()
ff.test()
 
 
 

About Author

Rohitesh Rawat

Rohitesh is an expert in Agile methodologies, specializing in Scrum. He possesses a wide range of skills, including proficiency in Jira, MongoDB, planning, scoping, process creation and management, and QA. Over the years, he has led the successful delivery of several offshore projects, including Konfer, Virgin Media, HP1T, and Transleqo. Rohitesh holds certifications as a Certified Scrum Master (CSM) and Project Management Professional (PMP) and has a comprehensive understanding of the entire Project Life Cycle (PLC).

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us