0
0
Selenium Pythontesting~15 mins

Why element interaction drives test scenarios in Selenium Python - Automation Benefits in Action

Choose your learning style9 modes available
Verify login functionality by interacting with input fields and buttons
Preconditions (2)
Step 1: Enter 'user@example.com' in the email input field
Step 2: Enter 'Password123' in the password input field
Step 3: Click the login button
Step 4: Wait for the dashboard page to load
✅ Expected Result: User is redirected to the dashboard page with URL containing '/dashboard' and a welcome message is displayed
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the URL contains '/dashboard' after login
Verify the welcome message element is visible on the dashboard page
Best Practices:
Use explicit waits to wait for elements to be interactable
Use meaningful locators like By.ID or By.NAME instead of brittle XPath
Separate page interactions into functions or page objects for clarity
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


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.

Common Mistakes - 3 Pitfalls
{'mistake': 'Using time.sleep() instead of explicit waits', 'why_bad': 'It causes tests to wait unnecessarily long or fail if the element loads slower or faster than expected.', 'correct_approach': "Use Selenium's explicit waits like WebDriverWait with expected_conditions to wait only as long as needed."}
Using brittle XPath locators that break easily
Not clearing input fields before sending keys
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials (valid and invalid) to verify login behavior.

Show Hint