Selenium Waits

Last Updated : 27 May, 2026

Selenium Waits help synchronize automation scripts with dynamic web applications. They make the script wait until a specific condition is met before interacting with web elements. This improves test stability and reduces failures caused by timing issues.

  • Ensures elements are loaded and ready before interaction.
  • Prevents test failures caused by asynchronous page loading.
  • Improves reliability and efficiency of Selenium test scripts.

Need for Selenium Waits

Selenium waits are important in automating web applications to handle the timing issues and ensure that our testing scripts are interacting with the element accurately. They help in addressing the delay in element availability, page loading, and rendering, making our testing script more reliable.

Example:

waits1
Selenium Waits

Here when we click on the create image button it takes some time to load the image, here if we use selenium click on the create image button and locate the image it will not be able to locate the image because the automation script will try to locate the image immediately and when it isn't available for interaction.

Python
from selenium import webdriver
from selenium.webdriver.common.by import By

driver=webdriver.Chrome()
url="http://127.0.0.1:5500/index.html"
driver.get(url)

try:
    button=driver.find_element(By.ID,"createImage")
    button.click()
    img=driver.find_element(By.CSS_SELECTOR,"#output img")
except:
    print("Not found")
else:
    print("found")

Output:

waits2
Selenium Waits

Explanation:

  • We used the find_element() method with the ID locator strategy to locate the "Create Image" button having the ID createImage, and then clicked on it.
  • Next, we used the find_element() method with the CSS_SELECTOR locator strategy to find the <img> tag inside the <div> element with the ID output.
  • However, the script failed to locate the image because the image was not immediately loaded after clicking the button. Since the image takes a few seconds to appear, Selenium tried to find it before it became available in the DOM.
  • As a result, the output was: "Not found".

So, in order to synchronize our automation script with the loading time of the element we'll have to use Selenium Waits.

Types of Selenium Waits

Selenium Waits are the mechanism that allows the automation script to pause the execution and wait until certain conditions are met before throwing an error if the element is not found. Selenium Waits helps to synchronize the execution script with the loading time of the website. There are two types of Waits In Selenium they are

  • Implicit Waits: Implicit Waits is a type of wait which instruct the Web Driver to wait for specific amount of time before throwing an error. It is applied globally.
  • Explicit Waits: Explicit wait is a type of wait which instructs the Web Driver to wait until a certain condition is met or maximum type is elapsed.

How to use Selenium Waits?

1. Implicit Waits

Implicit Wait is a type of wait in Selenium that instructs the WebDriver to wait for a specified amount of time before throwing a NoSuchElementException if an element is not found.

It is applied globally to all element search operations for the entire browser session. If the implicit wait is set to 10 seconds, WebDriver will wait up to 10 seconds for the element to appear before throwing an exception. If the element is found earlier, it immediately returns the element reference.

Syntax:

driver.implicitly_wait(time_in_seconds)
#replace time_in_second with the integer value of seconds.

Python
from selenium import webdriver
from selenium.webdriver.common.by import By

driver=webdriver.Chrome()
url=&quot;http://127.0.0.1:5500/index.html&quot;
driver.get(url)

#implicit wait of 5 seconds
driver.implicitly_wait(5)
try:
    button=driver.find_element(By.ID,&quot;createImage&quot;)
    button.click()
    img=driver.find_element(By.CSS_SELECTOR,&quot;#output img&quot;)
except:
    print(&quot;Not found&quot;)
else:
    print(&quot;found&quot;)

Output:

waits3
Selenium Waits

Explanation: Here we have set the implicit wait to 5 seconds so now before throwing an error Selenium Web Driver will wait for 5 seconds during which the image gets rendered and returns its reference, so the output is "found".

2. Explicit Waits

Explicit wait is a type of wait in Selenium which instructs the Web Driver to wait until a certain condition is met or maximum time is elapsed. Unlike Implicit Waits, Explicit waits are not applied globally they are more specific and allows the Web Driver to wait for a certain condition for a particular element before throwing an error.

Python
from selenium import webdriver
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()
url=&quot;http://127.0.0.1:5500/index.html&quot;
driver.get(url)
wait=WebDriverWait(driver,5)
try:
    button=driver.find_element(By.ID,&quot;createImage&quot;)
    button.click()
    img = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, &quot;#output img&quot;)))
except:
    print(&quot;Not found&quot;)
else:
    print(&quot;found&quot;)

Output:

waits4
Selenium Waits

Explanation:

  • WebDriverWait(driver, 5) creates an explicit wait with a maximum timeout of 5 seconds.
  • button.click() clicks the Create Image button.
  • wait.until(EC.presence_of_element_located(...)) waits until the image element is present in the DOM.
  • presence_of_element_located() is an Expected Condition used to check whether the element exists on the page.
  • The try-except block handles errors if the image is not found within 5 seconds.

3. Fluent Wait

Definition: Fluent Wait is an advanced type of Explicit Wait in Selenium. It provides additional flexibility by allowing users to define custom polling intervals and ignore specific exceptions while waiting for a condition.

Purpose: Fluent Wait is useful in complex synchronization scenarios where elements may take different amounts of time to load. It repeatedly checks for the element at regular intervals until the condition is met or the timeout occurs.

Syntax:

Wait<WebDriver> wait = new FluentWait<>(driver)

.withTimeout(Duration.ofSeconds(10))

.pollingEvery(Duration.ofSeconds(1))

.ignoring(NoSuchElementException.class);

WebElement element = wait.until(

ExpectedConditions.visibilityOfElementLocated(By.id("example")));

How It Works: Fluent Wait waits for a condition to become true by checking it repeatedly at defined polling intervals instead of continuously waiting for the entire timeout duration. It can also ignore specified exceptions during the waiting period.

Features:

  • Customizable polling intervals (e.g., check every second).
  • Ability to ignore certain exceptions during the wait period (like NoSuchElementException).

Advantages:

  • More control over how and when the condition is checked.
  • Ideal for complex, dynamic scenarios where elements might take variable times to load.

Disadvantages:

  • Slightly more complex to implement.
  • May lead to higher resource usage due to constant polling.

Definition: Thread.sleep() is a hard wait method that pauses the execution of the test script for a fixed amount of time.

Purpose: It is mainly used for debugging or temporary delays during test execution.

Syntax:

Thread.sleep(5000); // Waits for 5 seconds

Drawbacks:

  • Inefficient because it always waits for the full duration even if the element becomes ready earlier.
  • Makes test execution slower and less reliable.
  • Not recommended for production automation frameworks due to flaky test behavior.

Comparison of Wait Mechanisms in Selenium

Wait TypeDefinitionWaits ForScopeFlexibilityRecommended Use
Implicit WaitWaits for a specified time while searching for elementsElement presenceGlobal (applies to all elements)LowSimple synchronization for entire test session
Explicit WaitWaits for a specific condition before proceedingSpecific conditions like visibility or clickabilitySpecific element/conditionMediumDynamic elements with known conditions
Fluent WaitAdvanced explicit wait with polling and exception handlingSpecific conditions with custom intervalsSpecific element/conditionHighComplex dynamic applications
Thread.sleep()Pauses execution for a fixed timeFixed duration onlyEntire script executionNoneTemporary debugging only

Best Practices for Using Selenium Waits

Here are some best practices for using Selenium Waits which will help you in creating stable and reliable automation scripts.

  • Prefer Explicit Waits Over Implicit Waits: Explicit waits provide better control and are more specific, helping avoid unnecessary waiting for all elements.
  • Choose the Right Wait Condition: Use appropriate expected conditions such as element_to_be_clickable, visibility_of_element_located, or presence_of_element_located based on the testing scenario.
  • Set Appropriate Wait Times: Configure wait times according to the application’s behavior. The timeout should be long enough for reliability but not unnecessarily long.
  • Use Explicit Waits for Important Elements: Apply explicit waits for critical elements to ensure they are visible, present, and ready for interaction.
  • Handle Expected Exceptions Properly: Handle common exceptions like TimeoutException and StaleElementReferenceException to improve script stability.
  • Focus on Critical User Actions: Add waits around important user interactions such as clicking buttons, loading pages, or submitting forms.
  • Review and Update Waits Regularly: As the application changes over time, regularly update wait conditions and timeout values to maintain effective automation scripts.
Comment

Explore