CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-seleniumhq-selenium--selenium-api

Selenium WebDriver core API for automating web browsers across different platforms and programming languages.

Pending
Overview
Eval results
Files

elements.mddocs/

Element Interaction

The WebElement interface provides comprehensive functionality for interacting with HTML elements, including clicking, typing, querying properties, and taking screenshots.

Capabilities

WebElement Interface

Core interface for HTML element interaction extending SearchContext and TakesScreenshot.

/**
 * WebElement interface for HTML element interaction
 * Extends SearchContext for finding child elements and TakesScreenshot for element screenshots
 */
interface WebElement extends SearchContext, TakesScreenshot {
    /**
     * Click the element
     */
    void click();
    
    /**
     * Submit a form containing this element
     */
    void submit();
    
    /**
     * Type text into the element (usually input fields)
     * @param keysToSend - Text and special keys to send
     */
    void sendKeys(CharSequence... keysToSend);
    
    /**
     * Clear the content of the element (input fields and text areas)
     */
    void clear();
    
    /**
     * Get the HTML tag name of the element
     * @return Tag name in lowercase (e.g., "input", "div", "span")
     */
    String getTagName();
    
    /**
     * Get DOM attribute value
     * @param name - Attribute name
     * @return Attribute value or null if not present
     */
    String getDomAttribute(String name);
    
    /**
     * Get DOM property value
     * @param name - Property name  
     * @return Property value or null if not present
     */
    String getDomProperty(String name);
    
    /**
     * Get attribute value (legacy method, use getDomAttribute for new code)
     * @param name - Attribute name
     * @return Attribute value or null if not present
     */
    String getAttribute(String name);
    
    /**
     * Get ARIA role of the element
     * @return ARIA role or null if not present
     */
    String getAriaRole();
    
    /**
     * Get accessible name of the element
     * @return Accessible name or null if not present
     */
    String getAccessibleName();
    
    /**
     * Check if the element is selected (checkboxes, radio buttons, options)
     * @return true if selected, false otherwise
     */
    boolean isSelected();
    
    /**
     * Check if the element is enabled for interaction
     * @return true if enabled, false if disabled
     */
    boolean isEnabled();
    
    /**
     * Check if the element is visible on the page
     * @return true if displayed, false if hidden
     */
    boolean isDisplayed();
    
    /**
     * Get the visible text content of the element
     * @return Text content as seen by the user
     */
    String getText();
    
    /**
     * Get computed CSS property value
     * @param propertyName - CSS property name (e.g., "color", "font-size")
     * @return Computed CSS value
     */
    String getCssValue(String propertyName);
    
    /**
     * Get element position relative to top-left of page
     * @return Point with x,y coordinates
     */
    Point getLocation();
    
    /**
     * Get element dimensions
     * @return Dimension with width and height
     */
    Dimension getSize();
    
    /**
     * Get element position and dimensions combined  
     * @return Rectangle with position and size
     */
    Rectangle getRect();
}

Usage Examples:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.chrome.ChromeDriver;

WebDriver driver = new ChromeDriver();

try {
    driver.get("https://example.com/form");
    
    // Find and interact with input elements
    WebElement usernameField = driver.findElement(By.id("username"));
    WebElement passwordField = driver.findElement(By.id("password"));
    WebElement submitButton = driver.findElement(By.xpath("//button[@type='submit']"));
    
    // Type into input fields
    usernameField.sendKeys("testuser");
    passwordField.sendKeys("password123");
    
    // Clear field and retype
    usernameField.clear();
    usernameField.sendKeys("newuser");
    
    // Use special keys
    usernameField.sendKeys(Keys.CONTROL, "a"); // Select all
    usernameField.sendKeys("replacedtext");
    
    // Check element properties
    System.out.println("Tag name: " + usernameField.getTagName()); // "input"
    System.out.println("Type attribute: " + usernameField.getDomAttribute("type")); // "text"
    System.out.println("Value property: " + usernameField.getDomProperty("value"));
    System.out.println("Placeholder: " + usernameField.getAttribute("placeholder"));
    
    // Check element state
    if (submitButton.isEnabled() && submitButton.isDisplayed()) {
        submitButton.click();
    }
    
    // Work with checkboxes and radio buttons
    WebElement checkbox = driver.findElement(By.id("terms"));
    if (!checkbox.isSelected()) {
        checkbox.click();
    }
    
    // Get element information
    Rectangle rect = usernameField.getRect();
    System.out.println("Element position: " + rect.getX() + "," + rect.getY());
    System.out.println("Element size: " + rect.getWidth() + "x" + rect.getHeight());
    
    // Get CSS properties
    String color = usernameField.getCssValue("color");
    String fontSize = usernameField.getCssValue("font-size");
    
    // Get accessible information
    String ariaRole = submitButton.getAriaRole();
    String accessibleName = submitButton.getAccessibleName();
    
} finally {
    driver.quit();
}

SearchContext Interface

Element finding interface implemented by both WebDriver and WebElement for locating child elements.

/**
 * SearchContext interface for element finding
 * Implemented by WebDriver (searches entire page) and WebElement (searches within element)
 */
interface SearchContext {
    /**
     * Find the first element matching the given locator
     * @param by - Element locator strategy
     * @return First matching WebElement
     * @throws NoSuchElementException if no element found
     */
    WebElement findElement(By by);
    
    /**  
     * Find all elements matching the given locator
     * @param by - Element locator strategy
     * @return List of matching WebElements (empty list if none found)
     */
    List<WebElement> findElements(By by);
}

Usage Examples:

// Find elements from driver (entire page)
WebElement form = driver.findElement(By.id("loginForm"));
List<WebElement> allInputs = driver.findElements(By.tagName("input"));

// Find child elements within a parent element
WebElement navigation = driver.findElement(By.className("navbar"));
List<WebElement> navLinks = navigation.findElements(By.tagName("a"));
WebElement homeLink = navigation.findElement(By.linkText("Home"));

// Chain element searches
WebElement table = driver.findElement(By.id("dataTable"));
WebElement firstRow = table.findElement(By.xpath(".//tr[1]"));
List<WebElement> cells = firstRow.findElements(By.tagName("td"));

TakesScreenshot Interface

Screenshot capture capability for WebDriver and WebElement instances.

/**
 * TakesScreenshot interface for capturing screenshots
 * Implemented by WebDriver (full page) and WebElement (element only)
 */
interface TakesScreenshot {
    /**
     * Capture screenshot in specified output format
     * @param target - Output format (BASE64, BYTES, or FILE)
     * @return Screenshot data in requested format
     */
    <X> X getScreenshotAs(OutputType<X> target);
}

Usage Examples:

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

// Full page screenshot
TakesScreenshot screenshot = (TakesScreenshot) driver;
File fullPageFile = screenshot.getScreenshotAs(OutputType.FILE);
Files.copy(fullPageFile.toPath(), Paths.get("fullpage.png"));

// Element screenshot
WebElement loginForm = driver.findElement(By.id("loginForm"));
File elementFile = loginForm.getScreenshotAs(OutputType.FILE);
Files.copy(elementFile.toPath(), Paths.get("loginform.png"));

// Screenshot as base64 string (useful for embedding in reports)
String base64Screenshot = screenshot.getScreenshotAs(OutputType.BASE64);

// Screenshot as byte array
byte[] screenshotBytes = screenshot.getScreenshotAs(OutputType.BYTES);

Element State Queries

Common patterns for checking element states and properties.

Usage Examples:

WebElement element = driver.findElement(By.id("myElement"));

// Comprehensive element state check
public boolean isElementInteractable(WebElement element) {
    return element.isDisplayed() && element.isEnabled();
}

// Check if element has specific CSS class
public boolean hasClass(WebElement element, String className) {
    String classes = element.getAttribute("class");
    return classes != null && classes.contains(className);
}

// Get element text with fallback to value attribute
public String getElementText(WebElement element) {
    String text = element.getText();
    if (text.isEmpty()) {
        text = element.getAttribute("value");
    }
    return text;
}

// Check if input field is empty
public boolean isInputEmpty(WebElement input) {
    return input.getDomProperty("value").isEmpty();
}

// Wait for element to be clickable
WebElement button = driver.findElement(By.id("submitButton"));
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(button));
button.click();

Form Element Interactions

Specialized interactions for different form element types.

Usage Examples:

// Text input
WebElement textInput = driver.findElement(By.id("textField"));
textInput.clear();
textInput.sendKeys("New text value");

// Password input  
WebElement passwordInput = driver.findElement(By.id("password"));
passwordInput.sendKeys("secretpassword");

// Textarea
WebElement textarea = driver.findElement(By.tagName("textarea"));
textarea.clear();
textarea.sendKeys("Line 1\nLine 2\nLine 3");

// Checkbox
WebElement checkbox = driver.findElement(By.id("agreeTerms"));
if (!checkbox.isSelected()) {
    checkbox.click();
}

// Radio button
WebElement radioButton = driver.findElement(By.id("option1"));
radioButton.click();

// File upload
WebElement fileInput = driver.findElement(By.id("fileUpload"));
fileInput.sendKeys("/path/to/file.txt");

// Select dropdown (use Select class for better support)
WebElement selectElement = driver.findElement(By.id("country"));
Select select = new Select(selectElement);
select.selectByVisibleText("United States");
select.selectByValue("us");
select.selectByIndex(0);

Color Utility Class

Utility class for parsing and manipulating CSS color values from elements.

/**
 * Color class for CSS color parsing and manipulation
 * Converts between different color representations
 */
class Color {
    /**
     * Create Color from CSS color string
     * @param cssColorString - CSS color value (hex, rgb, rgba, name, etc.)
     * @return Color instance
     */
    static Color fromString(String cssColorString);
    
    /**
     * Get color as hex string
     * @return Hex color string (e.g., "#ff0000")
     */
    String asHex();
    
    /**
     * Get color as RGB string
     * @return RGB color string (e.g., "rgba(255, 0, 0, 1)")
     */
    String asRgb();
    
    /**
     * Get color as RGBA string
     * @return RGBA color string with alpha channel
     */
    String asRgba();
    
    /**
     * Get red component (0-255)
     * @return Red value
     */
    short getRed();
    
    /**
     * Get green component (0-255)
     * @return Green value
     */
    short getGreen();
    
    /**
     * Get blue component (0-255)
     * @return Blue value
     */
    short getBlue();
    
    /**
     * Get alpha/opacity component (0.0-1.0)
     * @return Alpha value
     */
    float getAlpha();
}

Usage Examples:

import org.openqa.selenium.support.Color;

// Get element color and parse it
WebElement element = driver.findElement(By.id("colorful-element"));
String cssColor = element.getCssValue("color");
Color color = Color.fromString(cssColor);

// Convert between color formats
String hexColor = color.asHex();        // "#ff0000"
String rgbColor = color.asRgb();        // "rgb(255, 0, 0)"
String rgbaColor = color.asRgba();      // "rgba(255, 0, 0, 1)"

// Get individual color components
short red = color.getRed();      // 255
short green = color.getGreen();  // 0
short blue = color.getBlue();    // 0
float alpha = color.getAlpha();  // 1.0

// Parse different CSS color formats
Color hexColor1 = Color.fromString("#ff0000");
Color hexColor2 = Color.fromString("#f00");
Color rgbColor1 = Color.fromString("rgb(255, 0, 0)");
Color rgbaColor1 = Color.fromString("rgba(255, 0, 0, 0.5)");
Color namedColor = Color.fromString("red");

// Verify element colors in tests
WebElement errorMessage = driver.findElement(By.className("error"));
Color errorColor = Color.fromString(errorMessage.getCssValue("color"));
assert errorColor.asHex().equals("#ff0000"); // Verify red color

Install with Tessl CLI

npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-api

docs

alerts.md

configuration.md

drivers.md

elements.md

index.md

interactions.md

javascript.md

locators.md

page-objects.md

waits.md

webdriver.md

tile.json