Bird
Raised Fist0
Selenium Pythontesting~5 mins

Absolute vs relative XPath in Selenium Python - Quick Revision & Key Differences

Choose your learning style10 modes available
Recall & Review
beginner
What is an absolute XPath?
An absolute XPath is a full path from the root element to the target element in the HTML document. It starts with a single slash (/). It shows the exact location of the element in the page structure.
Click to reveal answer
beginner
What is a relative XPath?
A relative XPath starts from anywhere in the document and finds the element based on conditions or attributes. It starts with double slashes (//). It is shorter and more flexible than absolute XPath.
Click to reveal answer
intermediate
Why is relative XPath preferred over absolute XPath in Selenium tests?
Relative XPath is preferred because it is less likely to break if the page layout changes. Absolute XPath depends on the full path, so small changes in the page can cause failures.
Click to reveal answer
beginner
Example of an absolute XPath for a button element:
/html/body/div[2]/div[1]/button[1]
This means starting from the root html, go to body, then second div, then first div inside it, then first button.
Click to reveal answer
beginner
Example of a relative XPath for a button with text 'Submit':
//button[text()='Submit']
This finds any button element with the exact text 'Submit' anywhere in the page.
Click to reveal answer
Which XPath starts with a single slash '/'?
ANeither
BAbsolute XPath
CBoth absolute and relative
DRelative XPath
Which XPath is more flexible and less likely to break when page layout changes?
AAbsolute XPath
BBoth are equally flexible
CRelative XPath
DNone of the above
What does the XPath '//input[@id="username"]' represent?
AInvalid XPath
BAbsolute path to input element with id 'username'
CRelative path to root element
DRelative path to any input element with id 'username'
Which XPath is easier to maintain in automated tests?
ARelative XPath
BAbsolute XPath
CBoth are equally easy
DDepends on the browser
What is a risk of using absolute XPath in tests?
AIt breaks easily if page structure changes
BIt cannot locate elements
CIt runs slower
DIt requires special browser settings
Explain the difference between absolute and relative XPath with examples.
Think about how each XPath starts and how they locate elements.
You got /5 concepts.
    Describe why using relative XPath is a better practice in Selenium test automation.
    Consider what happens when the page layout changes.
    You got /4 concepts.

      Practice

      (1/5)
      1. Which of the following best describes an absolute XPath?
      easy
      A. It starts from the root node and follows the full path to the element.
      B. It starts from any node and uses conditions to find the element.
      C. It uses CSS selectors instead of XML paths.
      D. It only works with HTML tables.

      Solution

      1. Step 1: Understand XPath types

        Absolute XPath always starts from the root node, indicated by a single slash (/), and follows the full path to the target element.
      2. Step 2: Compare with relative XPath

        Relative XPath starts from anywhere in the document, often using double slashes (//) and conditions to locate elements.
      3. Final Answer:

        It starts from the root node and follows the full path to the element. -> Option A
      4. Quick Check:

        Absolute XPath = full path from root [OK]
      Hint: Absolute XPath always starts with a single slash / [OK]
      Common Mistakes:
      • Confusing absolute with relative XPath syntax
      • Thinking relative XPath always starts at root
      • Assuming absolute XPath uses conditions only
      2. Which of the following is a correct syntax for a relative XPath to find a button with text 'Submit'?
      easy
      A. /html/body/button[text()='Submit']
      B. //button[text()='Submit']
      C. button[text()='Submit']
      D. //*[@id='submit']

      Solution

      1. Step 1: Identify relative XPath syntax

        Relative XPath uses double slashes (//) to search anywhere in the document for matching elements.
      2. Step 2: Check the options

        //button[text()='Submit'] uses //button[text()='Submit'], which correctly finds a button element with exact text 'Submit'. /html/body/button[text()='Submit'] is absolute XPath. button[text()='Submit'] misses the // and is invalid XPath. //*[@id='submit'] uses an attribute selector but does not specify text.
      3. Final Answer:

        //button[text()='Submit'] -> Option B
      4. Quick Check:

        Relative XPath uses // and conditions [OK]
      Hint: Relative XPath starts with // and uses conditions [OK]
      Common Mistakes:
      • Using single slash / for relative XPath
      • Omitting // before element name
      • Confusing attribute and text selectors
      3. Given the following HTML snippet:
      <div>
        <ul>
          <li>Home</li>
          <li>About</li>
          <li>Contact</li>
        </ul>
      </div>

      What will the XPath //ul/li[2] select?
      medium
      A. The second list item <li>About</li>
      B. The third list item <li>Contact</li>
      C. The first list item <li>Home</li>
      D. No element, XPath is invalid

      Solution

      1. Step 1: Understand the XPath expression

        The XPath //ul/li[2] selects the second
      2. child element of any <ul> element anywhere in the document.
      3. Step 2: Match with HTML structure

        The <ul> has three
      4. children: Home (1), About (2), Contact (3). So li[2] selects About.
      5. Final Answer:

        The second list item <li>About</li> -> Option A
      6. Quick Check:

        li[2] = second child element [OK]
      Hint: XPath [n] selects the nth child element [OK]
      Common Mistakes:
      • Confusing index starting at 0 instead of 1
      • Thinking //ul/li[2] selects second ul element
      • Assuming XPath is zero-based
      4. You wrote this XPath to find a login button:
      /html/body/div/button[@id='login']
      But your test fails to find the element after page update. What is the likely issue?
      medium
      A. XPath cannot find buttons.
      B. The XPath uses wrong attribute syntax.
      C. The button does not have an id attribute.
      D. The XPath is absolute and fragile; page structure changed breaking the path.

      Solution

      1. Step 1: Analyze the XPath type

        The XPath starts with a single slash /, so it is absolute and depends on the full path from root.
      2. Step 2: Understand fragility of absolute XPath

        If the page structure changes (like adding a div), the absolute path breaks and element is not found.
      3. Final Answer:

        The XPath is absolute and fragile; page structure changed breaking the path. -> Option D
      4. Quick Check:

        Absolute XPath breaks if page structure changes [OK]
      Hint: Avoid absolute XPath for dynamic pages [OK]
      Common Mistakes:
      • Blaming attribute syntax instead of XPath type
      • Assuming XPath cannot find buttons
      • Ignoring page structure changes
      5. You want to write a stable XPath to find a product name inside a list where the product has a price of '$20'. The HTML structure is:
      <div class='product'>
        <span class='name'>Product A</span>
        <span class='price'>$20</span>
      </div>
      <div class='product'>
        <span class='name'>Product B</span>
        <span class='price'>$30</span>
      </div>

      Which XPath is best to find the name of the product priced $20?
      hard
      A. //span[@class='price' and text()='$20']/preceding-sibling::span[@class='name']
      B. /html/body/div[1]/span[1]
      C. //div[@class='product'][span[@class='price' and text()='$20']]//span[@class='name']
      D. //div[@class='product']/span[@class='name']

      Solution

      1. Step 1: Understand the requirement

        We want the product name where the price is exactly '$20'. So we must find a product div with a price span of '$20' and then get its name span.
      2. Step 2: Evaluate each XPath

        //div[@class='product'][span[@class='price' and text()='$20']]//span[@class='name'] selects div with class 'product' having a span price '$20', then selects the name span inside it. This is precise and stable.
        /html/body/div[1]/span[1] is absolute XPath and fragile.
        //span[@class='price' and text()='$20']/preceding-sibling::span[@class='name'] tries to find price span then preceding sibling name span, which matches the HTML structure where name is before price.
        //div[@class='product']/span[@class='name'] selects all product names, ignoring price.
      3. Final Answer:

        //div[@class='product'][span[@class='price' and text()='$20']]//span[@class='name'] -> Option C
      4. Quick Check:

        Use relative XPath with conditions for stable element [OK]
      Hint: Use relative XPath with conditions to target elements precisely [OK]
      Common Mistakes:
      • Using absolute XPath that breaks easily
      • Selecting elements without conditions
      • Ignoring element hierarchy in XPath
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard