0
0
Selenium Pythontesting~15 mins

Expected conditions in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Wait for login button to be clickable and verify navigation
Preconditions (1)
Step 1: Wait until the login button with id 'login-btn' is clickable
Step 2: Click the login button
Step 3: Wait until the URL changes to 'https://example.com/dashboard'
Step 4: Verify that the dashboard page is displayed by checking the URL
✅ Expected Result: The login button is clicked only after it becomes clickable, and the user is navigated to the dashboard page with URL 'https://example.com/dashboard'
Automation Requirements - Selenium with Python
Assertions Needed:
Assert that the login button is clickable before clicking
Assert that the current URL is 'https://example.com/dashboard' after clicking
Best Practices:
Use WebDriverWait with expected_conditions for waiting
Avoid using time.sleep() for waiting
Use By.ID locator for the login button
Use explicit waits to handle dynamic page elements
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

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

    wait = WebDriverWait(driver, 10)  # wait up to 10 seconds

    # Wait until the login button is clickable
    login_button = wait.until(EC.element_to_be_clickable((By.ID, 'login-btn')))

    # Click the login button
    login_button.click()

    # Wait until the URL changes to dashboard
    wait.until(EC.url_to_be('https://example.com/dashboard'))

    # Assert the URL is correct
    assert driver.current_url == 'https://example.com/dashboard', f"Expected URL to be 'https://example.com/dashboard' but got {driver.current_url}"

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

First, it opens the login page URL.

Then, it uses WebDriverWait with the expected condition element_to_be_clickable to wait until the login button is ready to be clicked. This avoids errors from clicking too early.

After the button is clickable, it clicks the button.

Next, it waits until the URL changes to the dashboard page using url_to_be expected condition.

Finally, it asserts that the current URL matches the expected dashboard URL to confirm navigation succeeded.

This approach uses explicit waits instead of fixed delays, making the test more reliable and faster.

Common Mistakes - 3 Pitfalls
Using time.sleep() instead of explicit waits
Clicking the login button without waiting for it to be clickable
Using incorrect locator strategies like XPath with absolute paths
Bonus Challenge

Now add data-driven testing with 3 different login button IDs to verify the expected conditions work for each.

Show Hint