0
0
Selenium Javatesting~15 mins

Absolute vs relative XPath in Selenium Java - 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 'https://example.com/login'
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
✅ Expected Result: The login button is found and visible using both absolute and relative XPath locators
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify login button is displayed using absolute XPath
Verify login button is displayed using relative XPath
Best Practices:
Use explicit waits to wait for elements to be visible
Use By.xpath() locator with correct XPath syntax
Avoid hardcoding absolute XPath in real tests but demonstrate for learning
Keep code readable and maintainable
Automated Solution
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;

public class XPathTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        try {
            driver.get("https://example.com/login");

            // Absolute XPath example (not recommended for real tests)
            String absoluteXPath = "/html/body/div[2]/div/form/button";
            WebElement loginButtonAbsolute = wait.until(
                ExpectedConditions.visibilityOfElementLocated(By.xpath(absoluteXPath))
            );
            assert loginButtonAbsolute.isDisplayed() : "Login button not visible using absolute XPath";

            // Relative XPath example (recommended)
            String relativeXPath = "//button[@id='loginBtn']";
            WebElement loginButtonRelative = wait.until(
                ExpectedConditions.visibilityOfElementLocated(By.xpath(relativeXPath))
            );
            assert loginButtonRelative.isDisplayed() : "Login button not visible using relative XPath";

            System.out.println("Test passed: Login button found using both XPath types.");
        } finally {
            driver.quit();
        }
    }
}

This code opens the login page in Chrome browser.

It waits explicitly for the login button to be visible using two XPath locators:

  • Absolute XPath: Starts from the root html element and goes down the tree. This is fragile because any page structure change breaks it.
  • Relative XPath: Starts from anywhere in the document and uses attributes like id. This is more stable and recommended.

Assertions check if the button is visible for both locators.

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 correctness
Bonus Challenge

Now add data-driven testing with 3 different XPath locators for the login button and verify visibility for each.

Show Hint