Why element interaction drives test scenarios in Selenium Python - Automation Benefits in Action
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 def test_login_functionality(): driver = webdriver.Chrome() driver.get('https://example.com/login') wait = WebDriverWait(driver, 10) # Wait for email input to be visible and enter email email_input = wait.until(EC.visibility_of_element_located((By.ID, 'email'))) email_input.clear() email_input.send_keys('user@example.com') # Wait for password input to be visible and enter password password_input = wait.until(EC.visibility_of_element_located((By.ID, 'password'))) password_input.clear() password_input.send_keys('Password123') # Wait for login button to be clickable and click login_button = wait.until(EC.element_to_be_clickable((By.ID, 'loginBtn'))) login_button.click() # Wait for URL to contain '/dashboard' wait.until(EC.url_contains('/dashboard')) # Verify welcome message is visible welcome_message = wait.until(EC.visibility_of_element_located((By.ID, 'welcomeMsg'))) assert welcome_message.is_displayed(), 'Welcome message is not displayed' driver.quit()
This test script uses Selenium WebDriver with Python to automate the login scenario.
First, it opens the login page URL.
It uses explicit waits to wait for the email and password input fields to be visible before interacting with them. This ensures the elements are ready for input.
It clears any existing text and enters the provided credentials.
Then it waits for the login button to be clickable before clicking it, ensuring the button is ready to receive the click.
After clicking, it waits for the URL to contain '/dashboard' to confirm navigation to the dashboard page.
Finally, it waits for the welcome message element to be visible and asserts it is displayed, confirming successful login.
Using explicit waits and meaningful locators makes the test reliable and easy to maintain.
Now add data-driven testing with 3 different sets of login credentials (valid and invalid) to verify login behavior.