0
0
Selenium Pythontesting~15 mins

Time.sleep vs proper waits in Selenium Python - Automation Approaches Compared

Choose your learning style9 modes available
Verify login button becomes clickable after page load
Preconditions (2)
Step 1: Open the browser and navigate to the login page URL
Step 2: Wait for the login button to become clickable
Step 3: Click the login button
✅ Expected Result: The login button is clicked successfully only after it becomes clickable, without using fixed time delays
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the login button is clickable before clicking
Verify no fixed time.sleep is used for waiting
Verify the page URL remains the login page before clicking
Best Practices:
Use explicit waits (WebDriverWait) instead of time.sleep
Use proper locators like By.ID or By.CSS_SELECTOR
Handle exceptions if element is not found or not clickable
Automated Solution
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
from selenium.common.exceptions import TimeoutException

# Initialize the Chrome driver
with webdriver.Chrome() as driver:
    driver.get('https://example.com/login')

    try:
        # Wait up to 10 seconds for the login button to be clickable
        wait = WebDriverWait(driver, 10)
        login_button = wait.until(EC.element_to_be_clickable((By.ID, 'login-button')))

        # Assert the current URL is still the login page
        assert 'login' in driver.current_url, f"Expected 'login' in URL but got {driver.current_url}"

        # Click the login button
        login_button.click()

        print('Test Passed: Login button clicked after becoming clickable')
    except TimeoutException:
        print('Test Failed: Login button was not clickable within 10 seconds')
    except AssertionError as e:
        print(f'Test Failed: {e}')

This script uses Selenium WebDriver with Python to automate the test case.

First, it opens the browser and navigates to the login page URL.

Then, it uses WebDriverWait with expected_conditions.element_to_be_clickable to wait up to 10 seconds for the login button to become clickable. This is better than using time.sleep because it waits only as long as needed.

Before clicking, it asserts that the URL still contains 'login' to ensure we are on the correct page.

If the button becomes clickable, it clicks it and prints a success message.

If the button does not become clickable in time, or the URL check fails, it prints a failure message.

Using explicit waits makes the test more reliable and faster compared to fixed delays.

Common Mistakes - 3 Pitfalls
Using time.sleep to wait for elements
Using hardcoded XPath locators that are brittle
Not handling exceptions when element is not found or not clickable
Bonus Challenge

Now add data-driven testing with 3 different login page URLs to verify the login button on each page.

Show Hint