0
0
Selenium Pythontesting~15 mins

Find element by class name in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify login button is clickable by class name
Preconditions (1)
Step 1: Locate the login button using its class name 'btn-login'
Step 2: Click the login button
Step 3: Verify that the page navigates to the dashboard URL 'https://example.com/dashboard'
✅ Expected Result: The login button is found by class name, clicked successfully, and the user is redirected to the dashboard page
Automation Requirements - Selenium with Python
Assertions Needed:
Assert the login button is present and clickable
Assert the current URL after clicking is 'https://example.com/dashboard'
Best Practices:
Use explicit waits to wait for the login button to be clickable
Use By.CLASS_NAME locator strategy
Handle exceptions if element is not found
Keep code readable and maintainable
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

# Setup WebDriver (assuming chromedriver is in PATH)
driver = webdriver.Chrome()

try:
    # Open login page
    driver.get('https://example.com/login')

    # 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.CLASS_NAME, 'btn-login')))

    # Click the login button
    login_button.click()

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

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

    print('Test Passed: Login button clicked and dashboard loaded')

except TimeoutException:
    print('Test Failed: Element not found or page did not load in time')

finally:
    driver.quit()

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

First, it opens the login page URL.

Then it waits explicitly up to 10 seconds for the login button to be clickable using By.CLASS_NAME locator with the class name 'btn-login'. This ensures the element is ready before clicking.

After clicking the button, it waits until the URL changes to the expected dashboard URL.

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

TimeoutException is caught to handle cases where the element is not found or page load is slow.

The driver is closed in the finally block to clean up resources.

Common Mistakes - 4 Pitfalls
{'mistake': 'Using time.sleep() instead of explicit waits', 'why_bad': 'Hardcoded sleeps can make tests slow and flaky because they wait fixed time regardless of element readiness.', 'correct_approach': "Use Selenium's explicit waits like WebDriverWait with expected_conditions to wait only as long as needed."}
{'mistake': 'Using incorrect locator like By.ID when element only has class name', 'why_bad': "Locator won't find the element, causing NoSuchElementException.", 'correct_approach': 'Use By.CLASS_NAME with the exact class name to locate the element.'}
Not handling exceptions when element is not found
Clicking element before it is clickable
Bonus Challenge

Now add data-driven testing with 3 different class names for buttons on different pages

Show Hint