0
0
Selenium Pythontesting~5 mins

Time.sleep vs proper waits in Selenium Python

Choose your learning style9 modes available
Introduction

We wait in tests to let the web page load or elements appear before interacting with them. Using the right wait helps tests run smoothly and avoid errors.

When a page takes time to load and you want to wait before clicking a button.
When an element appears after some delay and you want to check it exists.
When you want to avoid your test failing because it tried to use something too early.
When you want your test to be faster by waiting only as long as needed, not fixed time.
Syntax
Selenium Python
import time

time.sleep(seconds)  # pauses test for fixed seconds

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, timeout).until(EC.condition(locator))  # waits until condition is true or timeout

time.sleep(seconds) pauses the test for exactly the seconds you give, no matter what.

WebDriverWait waits only as long as needed until the element or condition is ready, up to a maximum timeout.

Examples
This pauses the test for 5 seconds always, even if the page loads faster.
Selenium Python
import time

time.sleep(5)  # wait 5 seconds no matter what
This waits up to 10 seconds for the element with ID 'submit' to be visible. If it appears sooner, the test continues immediately.
Selenium Python
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.ID, 'submit')))
Sample Program

This script shows two ways to wait for a button that appears after some delay. The first uses time.sleep which always waits 5 seconds. The second uses WebDriverWait which waits up to 10 seconds but continues as soon as the button is visible.

Selenium 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
import time

# Setup driver (example with Chrome)
driver = webdriver.Chrome()
driver.get('https://example.com')

# Using time.sleep (bad practice)
print('Waiting with time.sleep...')
time.sleep(5)  # waits fixed 5 seconds
try:
    button = driver.find_element(By.ID, 'delayed-button')
    print('Button found after sleep')
except Exception as e:
    print('Button not found after sleep')

# Using proper wait (good practice)
print('Waiting with WebDriverWait...')
wait = WebDriverWait(driver, 10)
try:
    button = wait.until(EC.visibility_of_element_located((By.ID, 'delayed-button')))
    print('Button found with proper wait')
except Exception as e:
    print('Button not found with proper wait')

# Cleanup
driver.quit()
OutputSuccess
Important Notes

Avoid using time.sleep because it slows tests and can cause failures if the wait time is too short or too long.

Proper waits like WebDriverWait make tests faster and more reliable by waiting only as long as needed.

Always use explicit waits for elements that load dynamically or after actions.

Summary

time.sleep pauses for a fixed time no matter what.

Proper waits wait only as long as needed up to a timeout.

Use proper waits to make tests faster and less flaky.