0
0
Selenium Pythontesting~15 mins

Find element by XPath in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify login button is clickable using XPath locator
Preconditions (3)
Step 1: Open the browser and navigate to 'https://example.com/login'
Step 2: Locate the login button using XPath '//button[@id="loginBtn"]'
Step 3: Click the login button
Step 4: Verify that the URL changes to 'https://example.com/dashboard'
✅ Expected Result: The login button is found by XPath and clicked successfully, and the browser navigates to the dashboard page.
Automation Requirements - Selenium with Python
Assertions Needed:
Assert that the login button is found using XPath
Assert that after clicking, the current URL is 'https://example.com/dashboard'
Best Practices:
Use explicit waits to wait for the login button to be clickable
Use By.XPATH locator strategy
Close the browser after test execution
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 until the login button is clickable
    wait = WebDriverWait(driver, 10)
    login_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//button[@id="loginBtn"]')))

    # Assert the login button is found
    assert login_button is not None, "Login button not found by XPath"

    # Click the login button
    login_button.click()

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

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

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

First, it opens the Chrome browser and navigates to the login page URL.

It uses an explicit wait to wait up to 10 seconds for the login button to become clickable, locating it by XPath //button[@id="loginBtn"].

Then it asserts the button is found (not None), clicks it, and waits until the URL changes to the dashboard URL.

Finally, it asserts the current URL matches the expected dashboard URL.

The with statement ensures the browser closes automatically after the test.

Common Mistakes - 3 Pitfalls
{'mistake': 'Using time.sleep() instead of explicit waits', 'why_bad': 'Hardcoded sleeps can make tests slow and flaky if the element loads faster or slower than expected.', 'correct_approach': "Use Selenium's explicit waits like WebDriverWait with expected_conditions to wait only as long as needed."}
Using incorrect XPath syntax or absolute XPath
Not closing the browser after test
Bonus Challenge

Now add data-driven testing with 3 different XPath locators for login buttons on different pages

Show Hint