0
0
Selenium Pythontesting~15 mins

Absolute vs relative XPath in Selenium Python - Automation Approaches Compared

Choose your learning style9 modes available
Verify login button using absolute and relative XPath
Preconditions (2)
Step 1: Open the browser and navigate to the login page URL
Step 2: Locate the login button using absolute XPath and verify it is displayed
Step 3: Locate the login button using relative XPath and verify it is displayed
Step 4: Click the login button using relative XPath
Step 5: Verify that the next page URL contains '/dashboard'
✅ Expected Result: Login button is found and visible using both absolute and relative XPath. Clicking the button navigates to the dashboard page.
Automation Requirements - Selenium with Python
Assertions Needed:
Assert login button is displayed using absolute XPath
Assert login button is displayed using relative XPath
Assert current URL contains '/dashboard' after clicking login button
Best Practices:
Use explicit waits to wait for elements to be visible
Use relative XPath over absolute XPath for maintainability
Use Selenium's By class for locating elements
Avoid hardcoding absolute XPath in real tests, demonstrate difference only
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 (assuming chromedriver is in PATH)
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)

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

    # Step 2: Locate login button using absolute XPath
    absolute_xpath = '/html/body/div/main/form/button'
    login_button_abs = wait.until(EC.visibility_of_element_located((By.XPATH, absolute_xpath)))
    assert login_button_abs.is_displayed(), 'Login button not visible using absolute XPath'

    # Step 3: Locate login button using relative XPath
    relative_xpath = '//button[@id="loginBtn"]'
    login_button_rel = wait.until(EC.visibility_of_element_located((By.XPATH, relative_xpath)))
    assert login_button_rel.is_displayed(), 'Login button not visible using relative XPath'

    # Step 4: Click login button using relative XPath
    login_button_rel.click()

    # Step 5: Verify URL contains '/dashboard'
    wait.until(EC.url_contains('/dashboard'))
    assert '/dashboard' in driver.current_url, 'Did not navigate to dashboard after login'

finally:
    driver.quit()

This script opens the login page using Selenium WebDriver.

It first finds the login button using an absolute XPath, which starts from the root of the HTML document. This is fragile but shows how absolute XPath looks.

Then it finds the same button using a relative XPath, which is shorter and more maintainable.

Explicit waits ensure the button is visible before interacting.

After clicking the button using the relative XPath, the script waits until the URL contains '/dashboard' to confirm navigation.

Assertions check that elements are visible and navigation is successful.

Finally, the browser closes to clean up.

Common Mistakes - 3 Pitfalls
Using absolute XPath for all element locators
Not using explicit waits before accessing elements
Hardcoding XPath without verifying uniqueness
Bonus Challenge

Now add data-driven testing with 3 different login button IDs to verify locating buttons with different relative XPaths.

Show Hint