0
0
Selenium Javatesting~5 mins

Element dimensions and location in Selenium Java

Choose your learning style9 modes available
Introduction

Knowing where an element is on the page and its size helps us check if it looks right and works well.

To verify a button is visible and clickable on the screen.
To check if an image is displayed at the correct size.
To confirm a popup appears in the expected position.
To test responsive design by comparing element sizes on different devices.
To debug layout issues by checking element coordinates.
Syntax
Selenium Java
WebElement element = driver.findElement(By.id("elementId"));

// Get element location
Point location = element.getLocation();
int x = location.getX();
int y = location.getY();

// Get element size
Dimension size = element.getSize();
int width = size.getWidth();
int height = size.getHeight();

getLocation() returns the top-left corner position of the element.

getSize() returns the width and height of the element.

Examples
Prints the X and Y coordinates of the element on the page.
Selenium Java
Point location = element.getLocation();
System.out.println("X: " + location.getX() + ", Y: " + location.getY());
Prints the width and height of the element.
Selenium Java
Dimension size = element.getSize();
System.out.println("Width: " + size.getWidth() + ", Height: " + size.getHeight());
Checks if the element is wider than 100 pixels and prints a message.
Selenium Java
if (element.getSize().getWidth() > 100) {
    System.out.println("Element is wide enough.");
}
Sample Program

This test opens a webpage, finds an element by its ID, prints its position and size, and checks if the element has a valid size.

Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class ElementDimensionsLocationTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://example.com");
            WebElement element = driver.findElement(By.id("exampleElement"));

            Point location = element.getLocation();
            Dimension size = element.getSize();

            System.out.println("Element location: X=" + location.getX() + ", Y=" + location.getY());
            System.out.println("Element size: Width=" + size.getWidth() + ", Height=" + size.getHeight());

            // Simple assertion
            if (size.getWidth() > 0 && size.getHeight() > 0) {
                System.out.println("Test Passed: Element has valid size.");
            } else {
                System.out.println("Test Failed: Element size is invalid.");
            }
        } finally {
            driver.quit();
        }
    }
}
OutputSuccess
Important Notes

Coordinates (X, Y) start from the top-left corner of the page.

Element size can be zero if the element is hidden or not rendered.

Always close the browser with driver.quit() to free resources.

Summary

Use getLocation() to find where an element is on the page.

Use getSize() to get the width and height of an element.

Checking element size and location helps verify page layout and visibility.