0
0
Selenium Javatesting~20 mins

Absolute vs relative XPath in Selenium Java - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XPath Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding Absolute XPath
Which statement best describes an absolute XPath in Selenium?
AIt starts from the root node and specifies the full path to the element.
BIt starts from any node and uses // to find elements anywhere in the document.
CIt uses CSS selectors instead of XPath syntax.
DIt only works with elements having unique IDs.
Attempts:
2 left
💡 Hint
Think about how the path starts in the document tree.
Predict Output
intermediate
2:00remaining
Output of XPath Locator Usage
Given the following HTML snippet:
<html>
  <body>
    <div>
      <span id="target">Hello</span>
    </div>
  </body>
</html>

What will the following Selenium Java code return?
WebElement element = driver.findElement(By.xpath("/html/body/div/span"));
String text = element.getText();
System.out.println(text);
Anull
BNoSuchElementException
CEmpty string
DHello
Attempts:
2 left
💡 Hint
Check if the XPath matches the element exactly.
assertion
advanced
2:00remaining
Choosing the Correct XPath Assertion
You want to assert that an element located by relative XPath //button[@class='submit'] is visible on the page. Which assertion is best in Selenium Java?
AassertNull(driver.findElement(By.xpath("//button[@class='submit']")));
BassertEquals(driver.findElement(By.xpath("//button[@class='submit']")).getText(), "submit");
CassertTrue(driver.findElement(By.xpath("//button[@class='submit']")).isDisplayed());
DassertFalse(driver.findElement(By.xpath("//button[@class='submit']")).isEnabled());
Attempts:
2 left
💡 Hint
Visibility means the element is shown on the page.
🔧 Debug
advanced
2:00remaining
Debugging an XPath Locator Issue
The following code throws a NoSuchElementException:
WebElement element = driver.findElement(By.xpath("/html/body/div/button"));

Given the HTML:
<html>
  <body>
    <div>
      <section>
        <button>Click</button>
      </section>
    </div>
  </body>
</html>

What is the main reason for the exception?
AThe button element does not exist in the HTML.
BThe XPath is absolute but misses the &lt;section&gt; node in the path.
CThe XPath uses relative syntax but should be absolute.
DThe driver is not initialized.
Attempts:
2 left
💡 Hint
Check the full path in the HTML structure.
framework
expert
2:30remaining
Best Practice for XPath Usage in Test Frameworks
In a large Selenium Java test framework, which XPath usage practice improves test stability and maintenance the most?
AUse relative XPath with meaningful attributes and avoid absolute paths.
BUse absolute XPath for all elements to ensure exact location.
CAvoid XPath and use only CSS selectors everywhere.
DUse XPath with index positions like (//div)[3] to speed up tests.
Attempts:
2 left
💡 Hint
Think about what makes tests less fragile when UI changes.