While working with automation tools like Selenium, Protractor and so on we generally face situations where there is the inconsistent time taken for loading of a page and its web elements. Thus it leads to a situation wherein there exist sync issues between the elements on the page and the actions performed by Selenium.
The sync problems often lead to failures in the identification of elements due to the absence of that element in DOM leading to ElementNotVisibleException exception. In order to plug these loopholes, Selenium came up with the wait concepts that slows down the process of elements to be identified and tasks to be done on them.
The types of web driver waits are listed below:
Implicit wait in Selenium
The implicit wait is a dynamic wait which is applicable for every element in our test case. It is dynamic in nature because if we add an implicit wait of two seconds and the element is accessible at the first second itself, then we need not wait for the entire two seconds duration. As soon as the element is available in DOM, the control moves to the next step of test execution.
The implicit can be considered as a global wait as it is mostly applied to the test suite level. Timeout exceptions will be given if the element is not available after the implicit wait time elapsed. The implicit wait has a default time set to 0.
implicitly_wait (2), the time in the argument to the method is in seconds.
Code Implementation with Implicit wait.
from selenium import webdriver # setting the property for chromedriver.exe driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method for launching the application driver.get("https://www.softwaretestingmaterial.com/") # implicit wait set to 2 seconds driver.implicitly_wait(2) #to close the browser driver.close()
Thus the implicit wait is easy from an implementation perspective. However, it slows down the speed of execution and does not catch performance issues in the application. The implicit wait continues polling to a fixed time as specified by the user which remains applicable till the driver session is active.
Explicit wait in Selenium
The explicit wait is also dynamic wait which is applicable to the element on which it is applied. It is dynamic in nature because if we add an explicit wait of three seconds and the condition set for an element is met at the first second then we need not halt the execution for the entire three seconds before proceeding in the code.
In an explicit wait, we need to set some expected conditions to be satisfied by the element. If the criteria are not met in the time duration, a timeout exception is thrown. The WebDriverWait class and ExpectedCondition is used to create an explicit wait. The WebDriverWait automatically invokes the ExpectedCondition at the interval of 500 ms till the condition is returned true.
wt = WebDriverWait(driver, 2) wt.until (expected_conditions.element_to_be_clickable ((By.ID, "txt-bx")))
Some of the common expected conditions are listed below:
Code Implementation with Explicit wait.
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions from selenium.webdriver.support.wait import WebDriverWait # setting the property for chromedriver.exe driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method for launching the application driver.get("https://www.softwaretestingmaterial.com/") # implicit wait set to 2 seconds driver.implicitly_wait(2) # explicit wait set to 2 seconds wt = WebDriverWait(driver, 2) # expected condition title_is to be satisfied wt.(expected_conditions.title_is("Software Testing Material - Free Software Testing Tutorials & Videos")) #to close the browser driver.close()
Thus the explicit wait is difficult from an implementation perspective. However, it does not slow down the speed of execution and applies to the particular element of our choice.
In the next article, we will learn Webelement commands in Selenium Python.
- Selenium Python Tutorial
- Selenium Java Tutorial
- Selenium Interview Questions
- API Testing Tutorial
- Postman Tutorial