0
0
Selenium Pythontesting~15 mins

Why element location is the core skill in Selenium Python - Automation Benefits in Action

Choose your learning style9 modes available
Verify login button is clickable on the login page
Preconditions (1)
Step 1: Locate the login button using its id 'loginBtn'
Step 2: Click the login button
Step 3: Verify that the next page URL contains '/dashboard'
✅ Expected Result: Login button is found and clicked successfully, and user is redirected to the dashboard page
Automation Requirements - Selenium with Python
Assertions Needed:
Verify login button is present and clickable
Verify URL after click contains '/dashboard'
Best Practices:
Use explicit waits to wait for element presence and clickability
Use By.ID locator for stable element location
Avoid hardcoded sleeps
Handle exceptions if element not found
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

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

    wait = WebDriverWait(driver, 10)

    # Wait for login button to be clickable
    login_button = wait.until(EC.element_to_be_clickable((By.ID, 'loginBtn')))

    # Click the login button
    login_button.click()

    # Wait for URL to contain '/dashboard'
    wait.until(EC.url_contains('/dashboard'))

    # Assert URL contains '/dashboard'
    assert '/dashboard' in driver.current_url, f"Expected '/dashboard' in URL 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 waits explicitly for the login button to be clickable using WebDriverWait and expected_conditions. This avoids timing issues.

It locates the button by its stable id='loginBtn', which is a best practice for element location.

After clicking the button, it waits until the URL contains '/dashboard' to confirm navigation.

Finally, it asserts the URL contains '/dashboard' to verify the expected result.

This approach shows why locating elements correctly and waiting for them is core to reliable test automation.

Common Mistakes - 3 Pitfalls
Using time.sleep() instead of explicit waits
Using brittle locators like absolute XPath
Not handling element not found exceptions
Bonus Challenge

Now add data-driven testing with 3 different login button IDs to verify the button is clickable for each.

Show Hint