Explicit wait

Explicit Wait is defined as the maximum time to wait for a given condition before throwing an error.


It takes a total of four arguments.

  • Webdriver object : driver
  • Max wait time in seconds
  • Polling frequency time in seconds.This will search for a given webelement in a webpage for a given interval of time in seconds.
  • Exceptions which should ignored

After creating WebDriverWait, Now we need to call until() method which helps to wait until WebElement is present on the screen and perform the action on it.



wait = WebDriverWait(driver,25,poll_frequency=1,ignored_exceptions=[ElementNotVisibleException,NoSuchElementException])

wait.until(expected_conditions.presence_of_element_located((By.ID,"user_input")))


Example : If you declare ExplicitWait with a condition presence_of_element_located and max wait time is 25 seconds , if WebElement is visible less than 25 seconds then it will click or do the respective operation on that particular WebElement.


Example: In the below example we will launch the webpage and enter the text in editbox by implementing Explicit wait.

  

ExplicitWait.py

from selenium.webdriver.support import expected_conditions as ec
from selenium import webdriver
import time
from selenium.common.exceptions import ElementNotVisibleException, NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import  expected_conditions as ec

driver = webdriver.Chrome("/Users/admin/Documents/Skill2Lead/Others/drivers/chromedriver.exe")

driver.get("http://www.dummypoint.com/seleniumtemplate.html")
time.sleep(2)

wait = WebDriverWait(driver,25,poll_frequency=1,ignored_exceptions=[ElementNotVisibleException,NoSuchElementException])
ele = wait.until(ec.presence_of_element_located((By.ID,"user_input")))
ele.send_keys("Skill2Lead")

time.sleep(5)
driver.quit()