0
0
Selenium Javatesting~5 mins

Why selector mastery prevents fragile tests in Selenium Java

Choose your learning style9 modes available
Introduction

Good selectors help your tests find the right parts of a webpage every time. This stops tests from breaking when small page changes happen.

When writing tests that click buttons or fill forms on a webpage
When checking if a webpage shows the correct information
When automating repetitive tasks on websites
When maintaining tests that run often and must be reliable
Syntax
Selenium Java
By locator = By.method("value");

By is a Selenium class to find elements.

Common methods: id, name, cssSelector, xpath.

Examples
Finds element with id 'submit-button'. This is fast and reliable if id is unique.
Selenium Java
By idLocator = By.id("submit-button");
Finds a button with class 'primary'. Useful when id is missing.
Selenium Java
By cssLocator = By.cssSelector("button.primary");
Finds a link with text 'Home' inside a div with class 'menu'. XPath is flexible but can be fragile if page structure changes.
Selenium Java
By xpathLocator = By.xpath("//div[@class='menu']//a[text()='Home']");
Sample Program

This test opens a login page, fills username and password using strong selectors, clicks login, and checks if login succeeded by looking for a logout 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 SelectorMasteryTest {
    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");

            // Use strong selector by id
            WebElement username = driver.findElement(By.id("username"));
            username.sendKeys("testuser");

            // Use strong selector by css class
            WebElement password = driver.findElement(By.cssSelector("input.password-field"));
            password.sendKeys("password123");

            // Use strong selector by xpath
            WebElement loginButton = driver.findElement(By.xpath("//button[@type='submit']"));
            loginButton.click();

            // Check if login was successful by presence of logout button
            boolean loggedIn = driver.findElements(By.id("logout")).size() > 0;
            System.out.println("Login success: " + loggedIn);
        } finally {
            driver.quit();
        }
    }
}
OutputSuccess
Important Notes

Always prefer unique and stable attributes like id or name for selectors.

Avoid using selectors that depend on page layout or text that might change often.

Test selectors regularly to catch changes early and keep tests reliable.

Summary

Good selectors make tests stable and less likely to break.

Use unique attributes like id or name whenever possible.

Test and update selectors as the webpage changes.