Absolute XPath starts from the root element and follows the full path.
Relative XPath starts from anywhere in the document and uses // to search.
Examples
This is an absolute XPath. It starts from the root html and goes down the tree step by step.
Selenium Java
/html/body/div[1]/div[2]/input
This is a relative XPath. It finds any input element with an id of 'username' anywhere on the page.
Selenium Java
//input[@id='username']
Relative XPath that finds an a tag with text 'Home' inside a div with class 'menu'.
Selenium Java
//div[@class='menu']//a[text()='Home']
Sample Program
This Java Selenium code opens a login page and finds the username input field using both absolute and relative XPath. It prints the 'name' attribute of the found elements.
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class XPathExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/login");
// Using absolute XPath
WebElement absoluteElement = driver.findElement(By.xpath("/html/body/div[1]/form/input[1]"));
String absoluteValue = absoluteElement.getAttribute("name");
// Using relative XPath
WebElement relativeElement = driver.findElement(By.xpath("//input[@name='username']"));
String relativeValue = relativeElement.getAttribute("name");
System.out.println("Absolute XPath found element with name: " + absoluteValue);
System.out.println("Relative XPath found element with name: " + relativeValue);
driver.quit();
}
}
OutputSuccess
Important Notes
Absolute XPath is fragile. If the page layout changes, it may break.
Relative XPath is more flexible and preferred for most tests.
Use attributes and text in relative XPath to make locators stable and readable.
Summary
Absolute XPath starts from the root and follows the full path.
Relative XPath starts anywhere and uses // to find elements by attributes or text.
Relative XPath is usually better for stable and maintainable tests.
Practice
(1/5)
1. Which of the following best describes an absolute XPath?
easy
A. It uses CSS selectors to locate elements.
B. It starts anywhere in the document using // to find elements by attribute.
C. It starts from the root node and follows the full path to the element.
D. It only works with elements having unique IDs.
Solution
Step 1: Understand XPath types
Absolute XPath always starts from the root node, indicated by a single slash (/), and follows the full path down to the target element.
Step 2: Compare with relative XPath
Relative XPath starts with double slashes (//) and can start anywhere in the document, not necessarily from the root.
Final Answer:
It starts from the root node and follows the full path to the element. -> Option C
Quick Check:
Absolute XPath = full path from root [OK]
Hint: Absolute XPath starts with / from root, relative with // anywhere [OK]
Common Mistakes:
Confusing absolute with relative XPath syntax
Thinking relative XPath always starts from root
Assuming absolute XPath uses //
Believing XPath only works with unique IDs
2. Which of the following is a correct relative XPath syntax to find a button with text 'Submit'?
easy
A. //button[text()='Submit']
B. /html/body/button[text()='Submit']
C. button[text()='Submit']
D. //*[@id='Submit']
Solution
Step 1: Identify relative XPath syntax
Relative XPath uses // to search anywhere in the document. The expression //button[text()='Submit'] finds any button element with exact text 'Submit'.
Step 2: Check other options
/html/body/button[text()='Submit'] is absolute XPath, not relative. button[text()='Submit'] is invalid because it lacks // or /. //*[@id='Submit'] looks for an element with id='Submit', not text.
Final Answer:
//button[text()='Submit'] -> Option A
Quick Check:
Relative XPath uses // and matches element by text [OK]
Hint: Relative XPath starts with // and matches anywhere [OK]
//ul finds any <ul> element anywhere. li[2] selects the second <li> child of that <ul>.
Step 2: Identify the second <li> in the HTML
The <li> elements are: 1) Home, 2) About, 3) Contact. So li[2] is <li>About</li>.
Final Answer:
The second <li> element inside <ul> -> Option A
Quick Check:
//ul/li[2] = second list item [OK]
Hint: li[n] selects nth child li under the matched parent [OK]
Common Mistakes:
Confusing li[2] with the first li
Thinking XPath selects ul element
Assuming XPath is invalid syntax
Mixing absolute and relative XPath concepts
4. You wrote this XPath to find a login button: /html/body/div/button[@id='loginBtn'] But your test fails when the page structure changes slightly. What is the best fix?
medium
A. Add more parent nodes to the XPath
B. Keep the same XPath; the failure is unrelated
C. Use CSS selector instead of XPath
D. Change to relative XPath: //button[@id='loginBtn']
Solution
Step 1: Analyze the problem with absolute XPath
Absolute XPath starts from root and depends on full path. If page structure changes, the path breaks and test fails.
Step 2: Use relative XPath for stability
Relative XPath like //button[@id='loginBtn'] finds the button anywhere by id, making tests more stable and maintainable.
Final Answer:
Change to relative XPath: //button[@id='loginBtn'] -> Option D
Quick Check:
Relative XPath is more stable than absolute [OK]
Hint: Use // and attributes for stable locators, not full paths [OK]
Common Mistakes:
Adding more parents makes XPath fragile
Ignoring XPath type causes flaky tests
Assuming CSS selector always better
Not updating XPath after page changes
5. You want to locate an input field inside a form, but the form's position in the page changes often. Which XPath is best to locate the input with name='email' reliably?
hard
A. /html/body/div[2]/form/input[@name='email']
B. //form//input[@name='email']
C. /form/input[@name='email']
D. //input[@id='emailField']
Solution
Step 1: Evaluate absolute XPath option
/html/body/div[2]/form/input[@name='email'] is absolute and depends on exact page structure, which changes often, so it's fragile.
Step 2: Evaluate relative XPath options
//form//input[@name='email'] finds input with name='email' anywhere inside any form, making it flexible and stable despite page changes. /form/input[@name='email'] is invalid because it starts from root but form is not root. //input[@id='emailField'] depends on id which may not exist or be different.
Final Answer:
//form//input[@name='email'] -> Option B
Quick Check:
Relative XPath with attributes inside form is best [OK]
Hint: Use //form//input[@name='email'] for flexible, stable locating [OK]
Common Mistakes:
Using absolute XPath that breaks on layout changes
Assuming input has id when it may not
Starting XPath from root incorrectly
Not using // to search anywhere inside form
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.