Expected conditions in Selenium Python - Build an Automation Script
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.
Now add data-driven testing with 3 different login button IDs to verify the expected conditions work for each.