0
0
Selenium Javatesting~5 mins

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

Choose your learning style9 modes available
Introduction

XPath functions like contains and starts-with help find elements on a webpage when exact matches are hard to get. They make locating elements easier and more flexible.

When the element's attribute value changes slightly but has a common part.
When you want to find a button or link that starts with certain text.
When the full text or attribute is long but you know a unique part inside it.
When the webpage uses dynamic IDs or classes that partially stay the same.
When you want to avoid brittle tests that break if exact text changes.
Syntax
Selenium Java
//*[contains(@attribute, 'value')]
//*[starts-with(@attribute, 'value')]

contains() checks if an attribute or text includes a substring anywhere.

starts-with() checks if an attribute or text begins with a substring.

Examples
Finds any input element whose id attribute contains the word 'user'.
Selenium Java
//input[contains(@id, 'user')]
Finds any button element whose visible text starts with 'Log', like 'Login' or 'Logout'.
Selenium Java
//button[starts-with(text(), 'Log')]
Finds any link (a) whose href attribute contains 'contact'.
Selenium Java
//a[contains(@href, 'contact')]
Sample Program

This test opens a login page, finds the username input using contains() on the id, and finds the login button using starts-with() on the button text. It then types a username and clicks the button.

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 XPathFunctionsTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://example.com/login");

            // Using contains() to find username input
            WebElement username = driver.findElement(By.xpath("//input[contains(@id, 'user')]");
            username.sendKeys("testuser");

            // Using starts-with() to find login button
            WebElement loginButton = driver.findElement(By.xpath("//button[starts-with(text(), 'Log')]");
            loginButton.click();

            System.out.println("Test passed: Elements found and actions performed.");
        } catch (Exception e) {
            System.out.println("Test failed: " + e.getMessage());
        } finally {
            driver.quit();
        }
    }
}
OutputSuccess
Important Notes

Always check the webpage source to find stable parts of attributes or text for these functions.

Using these functions makes tests less fragile when small changes happen in the UI.

Be careful: too broad use of contains() can match multiple elements, causing errors.

Summary

contains() finds elements with attribute or text containing a substring.

starts-with() finds elements with attribute or text starting with a substring.

These functions help write flexible and reliable element locators in Selenium tests.