0
0
Selenium Pythontesting~10 mins

Time.sleep vs proper waits in Selenium Python - Test Execution Compared

Choose your learning style9 modes available
Test Overview

This test demonstrates the difference between using time.sleep() and Selenium's proper waits (WebDriverWait) to wait for an element to appear before interacting with it. It verifies that the element is clickable and the page behaves as expected.

Test Code - unittest
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
import unittest

class TestWaits(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/delayed_button')

    def test_time_sleep_wait(self):
        driver = self.driver
        # Using time.sleep to wait for button
        time.sleep(5)  # Wait fixed 5 seconds
        button = driver.find_element(By.ID, 'delayed-btn')
        button.click()
        message = driver.find_element(By.ID, 'message').text
        self.assertEqual(message, 'Button clicked!')

    def test_proper_wait(self):
        driver = self.driver
        # Using WebDriverWait to wait until button is clickable
        wait = WebDriverWait(driver, 10)
        button = wait.until(EC.element_to_be_clickable((By.ID, 'delayed-btn')))
        button.click()
        message = wait.until(EC.visibility_of_element_located((By.ID, 'message'))).text
        self.assertEqual(message, 'Button clicked!')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 9 Steps
StepActionSystem StateAssertionResult
1Test starts and browser opens ChromeBrowser window opens at URL https://example.com/delayed_button-PASS
2test_time_sleep_wait: Waits fixed 5 seconds using time.sleep(5)Page loads, button appears after 3 seconds delay-PASS
3Finds button element by ID 'delayed-btn'Button is present and visible-PASS
4Clicks the buttonButton click triggers message display-PASS
5Finds message element by ID 'message' and reads textMessage 'Button clicked!' is visibleAssert message text equals 'Button clicked!'PASS
6test_proper_wait: Uses WebDriverWait to wait until button is clickableWaits up to 10 seconds, button appears after 3 seconds-PASS
7Clicks the button after wait confirms clickableButton click triggers message display-PASS
8Waits until message element is visible and reads textMessage 'Button clicked!' is visibleAssert message text equals 'Button clicked!'PASS
9Test ends and browser closesBrowser window closes-PASS
Failure Scenario
Failing Condition: Using time.sleep with too short wait before button appears
Execution Trace Quiz - 3 Questions
Test your understanding
Why is using WebDriverWait better than time.sleep in Selenium tests?
ABecause WebDriverWait waits only as long as needed until condition is met
BBecause time.sleep is faster than WebDriverWait
CBecause WebDriverWait ignores element visibility
DBecause time.sleep waits for user input
Key Result
Use explicit waits like WebDriverWait to wait dynamically for elements instead of fixed time.sleep. This makes tests faster, more reliable, and less flaky.