Different Types of Selenium Wait – All You Need To Know
Missing elements during software testing are a menace for developers. But why does this happen? When a web page opens in the browser, the elements you want to interact with may load at different intervals of time. Due to this variation in the time lag, it becomes difficult to identify the elements, which may result in the exception, ‘ElementNotVisibleException’ being thrown by the Selenium WebDriver.
We use the wait command in Selenium to tackle this issue. In this blog, we will learn about Selenium wait and how different types of wait commands work in Selenium.
What is Wait in Selenium?
The Selenium wait command helps in observing and troubleshooting issues that arise due to variations in time lag for loading web elements during the automated testing of websites. It directs the test execution to pause for a certain length of time before it moves to the next step or throws the ElementNotVisibleException”. WebDriver, this way, can check if one or more elements are visible or clickable. To broaden your knowledge about other commands in Selenium such as Dropdown, Navigate, Window, Switch, Cookies, etc., you can go through this software testing course.
Different Types of Wait in Selenium
There are three types of Selenium wait commands, all of which vary from each other in their functions and can be used according to the type of element you want to work on. These commands are:
1. Implicit Wait in Selenium
It tells the WebDriver to wait for a certain amount of time during automation testing before it throws ‘NoSuchElementException’.
- The time is set to 0 by default.
- The WebDriver, upon receiving the command will wait till the specified time before throwing the exception.
- Implicit wait accepts two values.
- Enter time as an integer value in the first parameter
- In the second parameter and in terms of days, hours, minutes, seconds, milliseconds, etc
The syntax to understand the two parameters is:
driver.manage().timeouts(),implicitlyWait(TimeOut, TimeUnit.SECONDS);
Let us look at an 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
- Create a new instance of the Chrome driver
driver = webdriver.Chrome()
- Set the implicit wait time to 10 seconds
driver.implicitly_wait(10)
- Navigate to a web page
driver.get("https://www.example.com")
Try:
- Wait for the element with id “my-element” to be visible
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, "my-element"))
)
- Once the element is found, perform some actions
element.click()
- Other actions on the element
except Exception as e:
print("Element not found within the specified time:", str(e))
- Close the browser
driver.quit()
Here we have:
- Set the implicit wait time to 10 seconds using ‘driver.implicitly_wait(10)’.
- It means that if Selenium cannot find the element, it will wait for a maximum of ten seconds and then show an exception following the next command.
- Eventually, it will close the browser, as mentioned in the ‘driver.quit()’ command.
2. Explicit Wait in Selenium
It tells the WebDriver to wait until certain conditions are met or the maximum time is exceeded before giving an ‘ElementNotVisibleException’. The explicit wait is a smart command since it saves the test from being delayed unnecessarily. We have to declare in explicit wait the ‘ExpectedConditions’ we want to use.
The syntax for the same is as follows:
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut);
After this, we will give the wait command with the expected conditions using the following syntax:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("write XPath here.”)));
Let us look at an 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
- Create a new instance of the Chrome driver
driver = webdriver.Chrome()
- Navigate to a web page
driver.get("https://www.example.com")
Try:
- Wait for the element with id “my-element” to be visible
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, "my-element"))
)
- Once the element is found, perform some actions
element.click()
- Other actions on the element
except Exception as e:
print("Element not found within the specified time:", str(e))
- Close the browser
driver.quit()
‘WebDriverWait’ command will make the test explicitly:
- Wait for the id “my-element” to be visible on the web page.
- ‘WebDriverWait(driver, 10)’ specifies a maximum of ten seconds of waiting time.
- Using the ‘until()’ method along with ‘EC.visibility_of_element_located’ to define the condition for waiting until the element is visible.
- If the element is found then the operations can be performed on it. If not then the exception is thrown.
- At the end, the browser will be closed with ‘driver.quit()’ command.
3. Fluent Wait in Selenium
With it, we can fix the maximum time for which the WebDriver will wait for a condition to be met and also the frequency or the regular interval of time it will check if the condition appears before throwing ‘ElementNotVisibleException’. Fluent wait searches for the web element repeatedly at regular intervals until timeout or until the element is found. It is useful in case of elements that take a lot of time to load. There are two important components of this Selenium wait command:
a. Timeout Value: Defines the maximum time to wait for a condition.
b. Polling Frequency: It is the interval of time after which the condition is to be checked for success or failure.
The syntax for the fluent wait is as follows:
Syntax | Function |
Wait wait = new FluentWait(WebDriver reference) | It declares and initializes the fluent wait. |
wait.withTimeout(timeout, SECONDS) | It specifies the timeout of the wait. |
wait.pollingEvery(timeout, SECONDS) | It specifies polling time. |
wait.ignoring(Exception.class); | It specifies what exceptions to ignore. |
Let us look at an example.
- Create a new instance of the Chrome driver
driver = webdriver.Chrome()
Navigate to a web page
driver.get("https://www.example.com")
- Define the FluentWait with a maximum wait time of 30 seconds
fluent_wait = FluentWait(driver, timeout=30, poll_frequency=1)
Try:
- Wait until the element with id “my-element” is visible and clickable
element = fluent_wait.until(
EC.element_to_be_clickable((By.ID, "my-element"))
)
- Once the element is found, perform some actions
element.click()
- Other actions on the element
except TimeoutException:
print("Element not found within the specified time.")
- Close the browser
driver.quit()
‘FluentWait’ is used to define:
- More flexible wait conditions
- ‘timeout=30’ defines the maximum time to wait
- ‘frequency=1’ defines the polling frequency.
- ‘until()’ method in ‘FluentWait’ uses ‘EC.element_to_be_clickable’ to define the condition for waiting for the element to be visible.
- If the element is not found then ‘TimeoutException’ will be raised.
- And finally, the browser is closed with the ‘driver.quit()’ method.
Difference Between Implicit and Explicit Wait
Now that we have learned about different types of wait commands in Selenium, we must know about the difference between the implicit and explicit wait to know which one to use when. The following table will differentiate between the two to clear your confusion.
Implicit Wait | Explicit Wait | |
1. | It affects all the elements of the test script. | It affects only the specific elements that the user has mentioned. |
2. | Here, the user need not specify the ‘Expected Conditions’ in the code for the element to be located. | Here, the user has to specify the ‘Expected Conditions’ to be applied to the elements for them to be located. |
3. | It is more efficient in the test case where the elements are found within the period specified in the implicit wait. | It is more efficient in test cases where the elements are loading for a longer period. It helps to verify the property of the element, such as visibilityOfElementLocated, elementToBeSelected, etc. |
Conclusion
With the wait command in Selenium, you can detect the issues due to variations in time lag when the web page is loading and rectify them. Know that the key to software testing lies in its commands. Once you have mastered them, your software testing experience gets automatically enhanced.