0
0
Selenium Javatesting~20 mins

XPath axes (parent, following-sibling, preceding) in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XPath Axes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
locator
intermediate
2:00remaining
Identify the correct XPath using the parent axis
Given the HTML snippet below, which XPath expression correctly selects the
element that is the parent of the with text 'Price'?
Selenium Java
<div class="product">
  <span>Price</span>
</div>
A//span[text()='Price']/parent::div
B//span[text()='Price']/child::div
C//div[span='Price']/preceding-sibling::span
D//span[text()='Price']/following-sibling::div
Attempts:
2 left
💡 Hint
The parent axis selects the direct parent node of the current node.
locator
intermediate
2:00remaining
Select the correct following-sibling element
Which XPath expression selects the
  • element that immediately follows the
  • with text 'Item 2'?
  • Selenium Java
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
      <li>Item 4</li>
    </ul>
    A//li[text()='Item 2']/preceding-sibling::li[1]
    B//li[text()='Item 2']/parent::ul/li[3]
    C//li[text()='Item 2']/following::li[2]
    D//li[text()='Item 2']/following-sibling::li[1]
    Attempts:
    2 left
    💡 Hint
    The following-sibling axis selects siblings after the current node.
    Predict Output
    advanced
    2:00remaining
    What is the result of this XPath query?
    Given the HTML below, what text will be selected by the XPath expression //h2[text()='Chapter 2']/preceding-sibling::h2[1]?
    Selenium Java
    <div>
      <h2>Chapter 1</h2>
      <h2>Chapter 2</h2>
      <h2>Chapter 3</h2>
    </div>
    AChapter 2
    BChapter 3
    CChapter 1
    DNo nodes selected
    Attempts:
    2 left
    💡 Hint
    preceding-sibling selects siblings before the current node.
    assertion
    advanced
    2:30remaining
    Which assertion correctly verifies the presence of the parent element?
    In Selenium Java, which assertion correctly verifies that the parent of the element located by //span[@id='price'] has the class 'product'?
    Selenium Java
    WebElement priceSpan = driver.findElement(By.xpath("//span[@id='price']"));
    AAssert.assertTrue(priceSpan.findElement(By.xpath("child::div")).getAttribute("class").equals("product"));
    BAssert.assertEquals(priceSpan.findElement(By.xpath("parent::div")).getAttribute("class"), "product");
    CAssert.assertEquals(priceSpan.findElement(By.xpath("following-sibling::div")).getAttribute("class"), "product");
    DAssert.assertTrue(priceSpan.findElement(By.xpath("preceding-sibling::div")).getAttribute("class").contains("product"));
    Attempts:
    2 left
    💡 Hint
    Use the parent axis to find the parent element.
    🔧 Debug
    expert
    3:00remaining
    Debug the failing XPath locator using axes
    A test fails because the XPath //div[@class='menu']/following-sibling::ul/li does not find any elements. Given the HTML below, what is the main reason for failure?
    Selenium Java
    <div class="menu">Main Menu</div>
    <ul>
      <li>Home</li>
      <li>About</li>
    </ul>
    AThe <ul> is not a following sibling of the <div>, it is a following node but not sibling.
    BThe XPath should use preceding-sibling instead of following-sibling.
    CThe <ul> is a child of the <div>, so child axis should be used.
    DThe XPath syntax is invalid because axes cannot be used after attribute filters.
    Attempts:
    2 left
    💡 Hint
    Check the HTML structure to see sibling relationships.