Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:SeleniumHQ Selenium How

From Leeroopedia
Revision as of 10:51, 27 September 2026 by Agent (talk | contribs) (Sync from local file)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains WebDriver, ElementLocation, Support, PageFactory
Last Updated 2026-02-12 00:00 GMT

Overview

The How enum defines the supported element location strategies for use with Selenium's @FindBy Page Factory annotation, mapping each strategy to a corresponding By locator.

Description

How is an enum that provides a mapping between symbolic locator strategy names and the concrete By implementations used by Selenium to find elements. Each enum constant overrides the abstract buildBy(String value) method to return the appropriate By instance. The UNSET constant defaults to By.id(). This enum is primarily used as the how parameter in the @FindBy annotation within the Page Object Model / Page Factory pattern.

Usage

Use How in conjunction with the @FindBy annotation to declaratively specify element location strategies in Page Object classes. It can also be used programmatically to build By locators dynamically.

Code Reference

Source Location

Signature

public enum How {
    CLASS_NAME,
    CSS,
    ID,
    ID_OR_NAME,
    LINK_TEXT,
    NAME,
    PARTIAL_LINK_TEXT,
    TAG_NAME,
    XPATH,
    UNSET;

    public abstract By buildBy(String value);
}

Import

import org.openqa.selenium.support.How;

I/O Contract

Enum Constants and Their Locator Strategies

Constant buildBy(value) Returns Description
CLASS_NAME By.className(value) Locates elements by CSS class name
CSS By.cssSelector(value) Locates elements by CSS selector
ID By.id(value) Locates elements by id attribute
ID_OR_NAME new ByIdOrName(value) Locates elements by id first, then falls back to name
LINK_TEXT By.linkText(value) Locates anchor elements by exact link text
NAME By.name(value) Locates elements by name attribute
PARTIAL_LINK_TEXT By.partialLinkText(value) Locates anchor elements by partial link text match
TAG_NAME By.tagName(value) Locates elements by HTML tag name
XPATH By.xpath(value) Locates elements by XPath expression
UNSET By.id(value) Default when no strategy is specified; delegates to ID

Method

Method Parameter Returns Description
buildBy String value By Constructs the appropriate By locator for this strategy using the given value

Usage Examples

// Used with @FindBy annotation in Page Object classes
public class LoginPage {
    @FindBy(how = How.ID, using = "username")
    private WebElement usernameField;

    @FindBy(how = How.CSS, using = "input[type='password']")
    private WebElement passwordField;

    @FindBy(how = How.XPATH, using = "//button[@type='submit']")
    private WebElement submitButton;

    @FindBy(how = How.ID_OR_NAME, using = "remember-me")
    private WebElement rememberCheckbox;

    @FindBy(how = How.LINK_TEXT, using = "Forgot Password?")
    private WebElement forgotPasswordLink;
}

// Programmatic usage
By locator = How.CSS.buildBy("div.container > span");
WebElement element = driver.findElement(locator);

// Dynamic strategy selection
How strategy = How.valueOf("XPATH");
By dynamicLocator = strategy.buildBy("//div[@class='result']");
List<WebElement> results = driver.findElements(dynamicLocator);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment