0
0
Selenium Javatesting~10 mins

XPath functions (contains, starts-with) in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page and uses XPath functions contains and starts-with to find elements by partial attribute values. It verifies that the correct elements are found and clickable.

Test Code - JUnit with Selenium WebDriver
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 org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.time.Duration;

public class XPathFunctionsTest {
    WebDriver driver;
    WebDriverWait wait;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    @Test
    public void testContainsAndStartsWithXPath() {
        driver.get("https://example.com/testpage");

        // Find element with id containing 'submit'
        WebElement submitButton = wait.until(
            ExpectedConditions.presenceOfElementLocated(By.xpath("//button[contains(@id, 'submit')]")
            )
        );
        assertTrue(submitButton.isDisplayed());

        // Find element with class starting with 'nav-'
        WebElement navMenu = wait.until(
            ExpectedConditions.presenceOfElementLocated(By.xpath("//nav[starts-with(@class, 'nav-')]")
            )
        );
        assertTrue(navMenu.isDisplayed());

        // Click the submit button
        submitButton.click();
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open, ready to navigate-PASS
2Navigate to https://example.com/testpageBrowser loads the test page with buttons and navigation menu-PASS
3Wait until button with id containing 'submit' is present using XPath contains()Button element with id like 'btn-submit' is found in DOMCheck that submit button is displayedPASS
4Wait until nav element with class starting with 'nav-' is present using XPath starts-with()Navigation element with class like 'nav-main' is found in DOMCheck that navigation menu is displayedPASS
5Click the submit buttonSubmit button is clicked, triggering form submission or action-PASS
6Test ends and browser closesBrowser window closes cleanly-PASS
Failure Scenario
Failing Condition: The XPath locator does not find the element because the attribute value does not contain or start with the expected string
Execution Trace Quiz - 3 Questions
Test your understanding
Which XPath function is used to find elements by partial attribute value anywhere inside the string?
Aends-with()
Bstarts-with()
Ccontains()
Dequals()
Key Result
Using XPath functions like contains() and starts-with() helps locate elements with partial attribute matches, making tests more flexible and resilient to small changes in attribute values.