As we have learnt before, implicit waits are a method to make the test wait for the webpage’s components to load, so that they can be available during our tests. Selenium originally borrowed this idea from Watir. Now what does implicit wait does actually. Suppose we wrote a script like this
from selenium import webdriver import unittest class AlertHandle(unittest.TestCase): def setUp(self): self.driver=webdriver.Firefox() self.driver.implicitly_wait(30) self.driver.maximize_window() self.driver.get('http://www.questionselenium.com/2013/09/sample-confirmation-box.html') def test_Alterbox(self): print("This is a test to show implicit wait") def tearDown(self): self.driver.quit() if __name__=="__main__" unittest.main(versbosity=2)
implicitly_wait( ) method tells the script , more precisely, it tells the Webdriver to poll the DOM for a certain amount of time , here for 30 seconds, when trying to find an element or elements if they are not immediately available. Here by poll, we mean to check the DOM again and again.
Once defined, implicit wait will be defined for the whole life time of a Webdriver object instance, until it is changed. So once defined in a script, it will be active for the lifetime of a script, until modified. By default, the implicit wait time set is 0.
Also, implicit wait won’t allow you to set any predefined conditions to wait for any webelement to be present or as such. If you want such kinda flexibility, you need to take the help of explicit wait, which is why we are going to discuss this in next section.
Not in every situation you want a predefined wait to be available in tests and wait for that long duration, for the element to be present or be visible in the DOM. We want to define a wait condition, so that we can be able to wait for only that specific web element to be present and then carry on with the script functioning. To achieve this Selenium webdriver API provides us with explicit waits.
Explicit waits are a good way to organize a test script and provide more flexibility, by allowing us to design out tests in a way, where we can wait for some predefined or custom conditions and then carry on with what we want.
Explicit waits are implemented using the
expected_conditions class of the python/Java API.
Let see an suppose we have a JS alert on a webpage, and we want to see if that clicking on that button, which fires the alert , actually fires the alert on not. Now, we would want this test to wait to click on the button and then only wait for that amount of time when the alert is not present. Once the alert appears, we would just want to accept it and then go on and accept the alert.
import unittest from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions class Explicit_waiting(unittest.TestCase): def setUp(self): self.driver=webdriver.Firefox() self.driver.maximize_window() #getting the browser navigation self.driver.get('') def test_BrowserNav(self): driver=self.driver tt1=driver.find_element_by_id('timingAlert') tt1.click() WebDriverWait(self.driver,10).until(expected_conditions.alert_is_present()) alert1=driver.switch_to.alert alert1.accept() def tearDown(self): self.driver.quit() if __name__="__main__": unittest.main(verbosity=2)
Now, focus specifically on this line of code
Here, we are using the
WebDriverWait to make the driver wait a maximum of 10 seconds, until, the alert shows up. If the alert doesn’t appear in that stipulated time, the Webdriver will throw an
During the course of test execution, Webdriver calls the ExpectedCondition every 500 milliseconds, untill it returns successfully.
Expected Conditions :
These are some common conditions which are frequently encountered when automating web applications.
Selenium Python binding provides some convenience methods so you don’t have to code an expected_condition class yourself or create your own utility package for them.
You can read about the whole list of the expected conditions in the official Selenium python documentation here.