0
0
Selenium Pythontesting~15 mins

XPath with attributes in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify login button is clickable using XPath with attributes
Preconditions (1)
Step 1: Locate the login button using XPath with attribute id='loginBtn'
Step 2: Click the login button
Step 3: Verify that the URL changes to 'https://example.com/dashboard'
✅ Expected Result: The login button is found using XPath with attribute, clicked successfully, and the page navigates to the dashboard URL
Automation Requirements - Selenium with Python
Assertions Needed:
Assert the login button is present and clickable
Assert the current URL is 'https://example.com/dashboard' after clicking
Best Practices:
Use explicit waits to wait for the login button to be clickable
Use By.XPATH locator with attribute selector
Avoid hardcoded sleeps
Use try-except to handle element not found exceptions gracefully
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 login button to be clickable using XPath with attribute id='loginBtn'
    wait = WebDriverWait(driver, 10)
    login_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@id='loginBtn']")))

    # 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 URL to be dashboard 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. The button is located using an XPath expression that selects a button element with the attribute id='loginBtn'. This shows how to use XPath with attributes.

After the button is found and clickable, the script clicks it.

Next, it waits until the URL changes to the expected dashboard URL, confirming navigation succeeded.

Finally, it asserts the current URL matches the expected dashboard URL. If all steps succeed, it prints a success message; otherwise, it catches timeout exceptions and prints a failure message.

The script uses best practices like explicit waits, proper locators, and exception handling to make the test reliable and clear.

Common Mistakes - 3 Pitfalls
Using time.sleep() instead of explicit waits
Using XPath without attributes or using absolute XPath
Not handling exceptions when element is not found
Bonus Challenge

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

Show Hint