Selenium is an incredibly useful tool for automating web testing, but dealing with web elements that load dynamically can sometimes be tricky. This is where Selenium waits come into play.
- Selenium Features
- Selenium Installation
- Selenium Locators
- XPath Fundamentals
- CSS Selectors Methods
- Different Browsers Execution
- find_element & find_elements
- Check Enabled Status
- Check Displayed Status
- Check Selected Status
- Selenium Waits
- Send_keys Method
- Click Method
- Get Text
- Get Attribute Value
- Get Current URL
- Forward, Back, Refresh
- Take Screenshot
- Handle Browser Tabs
- Handle iframe
- Mouse Hover
- Context-Click
- Drag & Drop
- Handle Alerts
- Handle Dropdown
- Execute Javascript
- Scroll To element
- Headless Mode Execution
- Chrome Options
- Keyboard Action
Understanding Selenium Waits
Selenium waits allow you to manage timing issues and ensure that your script interacts with elements only when they’re ready. In this article, we’ll explore the different types of waits in Selenium, how to use them, and why they are essential in building robust automated tests.
Why Do We Need Waits in Selenium?
One of the biggest challenges in web automation is dealing with dynamic content that loads at different speeds. If your script tries to interact with an element that hasn’t loaded yet, it can throw an error. To solve this, Selenium provides different types of waits to pause your script and give the page time to load before continuing.
Types of Selenium Waits
Selenium offers three primary types of waits to deal with dynamic web elements:
- Implicit Waits
- Explicit Waits
- Fluent Waits
Let’s break down how each of these waits works.
1. Implicit Waits Explained
An Implicit Wait tells Selenium to wait for a specific amount of time before throwing an exception if an element isn’t found. This is like setting a blanket timeout for all the elements in your script.
How Implicit Waits Work
When you set an implicit wait, Selenium will pause the script for the given time, say 10 seconds, until the desired element becomes available. If it finds the element within that time, it proceeds. If not, it throws an exception
2. Explicit Waits Explained
Unlike implicit waits, Explicit Waits let you specify certain conditions under which Selenium should wait. Instead of just waiting for an element, it waits for a specific condition, like “element to be clickable” or “element to be visible.”
How Explicit Waits Work
You can define a condition in your script, and Selenium will keep checking for that condition to be met before interacting with the element. You can set both the condition and the maximum wait time.
3. Fluent Waits Explained
Fluent Waits offer more control by allowing you to set a polling interval, which determines how frequently Selenium checks if the condition is met. This wait is a more advanced form of explicit wait.
How Fluent Waits Differ from Explicit Waits
While both are condition-based waits, Fluent Waits let you customize the wait by defining the frequency with which Selenium checks the condition. Fluent waits are useful when you expect a delay but don’t want Selenium to keep waiting unnecessarily.
Using Polling Intervals with Fluent Waits
Fluent waits allow you to set a polling interval, like checking every 500 milliseconds, and can also ignore specific exceptions while waiting, which adds an extra layer of control to your tests.
Code Examples for Selenium Waits
Here are some practical examples of how to use different waits in Selenium with Python.
Using Implicit Waits in Python
from selenium import webdriver
driver = webdriver.Chrome()
driver.implicitly_wait(10) # Set an implicit wait of 10 seconds
driver.get("https://sqatools.in/dummy-booking-website/")
Using Explicit Waits in Python
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
driver = webdriver.Chrome()
# Lauch chrome browser and open dummy website
driver.get("https://sqatools.in/dummy-booking-website/")
# Wait up to 10 seconds for the element to be clickable
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, "firstname"))
)
element.click()
driver.close()
Using Fluent Waits in Python
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
from selenium.webdriver.support.ui import FluentWait
import time
driver = webdriver.Chrome()
driver.get("https://sqatools.in/dummy-booking-website/")
# Fluent wait example with a polling interval of 500 milliseconds
wait = WebDriverWait(driver, 10, poll_frequency=0.5, ignored_exceptions=[Exception])
# Wait for an element to be clickable
element = wait.until(EC.element_to_be_clickable((By.ID, "element_id")))
element.click()
driver.close()