Skip to main content

Fetch Dimensions of WebElement

COMPLETE WebElement Dimensions & Position - Selenium 4 Methods

Capturing height, width, and position of WebElements uses getRect() (Selenium 4) or legacy getSize() + getLocation() (Selenium 3).


getRect() returns a Rectangle object containing position (x,y) + size (width,height) in one call.

WebElement button = driver.findElement(By.id("submit-btn"));
Rectangle rect = button.getRect(); // Selenium 4

int x = rect.getX(); // Left position
int y = rect.getY(); // Top position
int width = rect.getWidth(); // Width
int height = rect.getHeight(); // Height

System.out.println("Button: x=" + x + ", y=" + y + ", w=" + width + ", h=" + height);

Output example:

Button: x=150, y=300, w=120, h=40

2. Selenium 3 Legacy Methods (Still Work)

Two separate calls - less efficient but backward compatible.

WebElement button = driver.findElement(By.id("submit-btn"));

// Position (top-left corner)
Point location = button.getLocation();
int x = location.getX();
int y = location.getY();

// Size (width + height)
Dimension size = button.getSize();
int width = size.getWidth();
int height = size.getHeight();

3. Complete Utility Class

public class ElementDimensions {
public static void printDimensions(WebElement element, String name) {
Rectangle rect = element.getRect();
System.out.printf("%s: x=%d, y=%d, width=%d, height=%d%n",
name, rect.getX(), rect.getY(),
rect.getWidth(), rect.getHeight());
}

public static boolean isVisibleInViewport(WebElement element) {
Rectangle rect = element.getRect();
return rect.getX() >= 0 && rect.getY() >= 0 &&
rect.getX() + rect.getWidth() <= 1920 && // Screen width
rect.getY() + rect.getHeight() <= 1080; // Screen height
}
}

Usage:

ElementDimensions.printDimensions(loginBtn, "Login Button");
ElementDimensions.printDimensions(header, "Page Header");

4. Real-World Examples

Verify Button Size

WebElement submitBtn = driver.findElement(By.id("submit"));
Rectangle rect = submitBtn.getRect();

assertEquals(120, rect.getWidth(), 10); // Width ±10px tolerance
assertEquals(40, rect.getHeight(), 5); // Height ±5px tolerance

Check Element Position (Layout Testing)

WebElement logo = driver.findElement(By.className("logo"));
Rectangle logoRect = logo.getRect();

// Logo should be top-left
assertTrue(logoRect.getX() < 100); // Left side
assertTrue(logoRect.getY() < 100); // Top side

Dynamic Element Sizing (Responsive Testing)

ChromeOptions options = new ChromeOptions();
options.addArguments("--window-size=375,667"); // Mobile viewport
WebDriver mobileDriver = new ChromeDriver(options);

WebElement btn = mobileDriver.findElement(By.id("responsive-btn"));
Rectangle rect = btn.getRect();
assertTrue(rect.getWidth() > 300); // Full width on mobile

5. JavaScript Alternative (More Precise)

getBoundingClientRect() - gets exact rendered dimensions including scroll position.[^6][^7]

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

Map<String, Object> rect = (Map<String, Object>) js.executeScript(
"var r = arguments[^0].getBoundingClientRect(); " +
"return {x: r.x, y: r.y, width: r.width, height: r.height};",
element
);

double x = (Double) rect.get("x");
double y = (Double) rect.get("y");
double width = (Double) rect.get("width");
double height = (Double) rect.get("height");

When to use JS:

  • Elements with CSS transforms, animations, or scroll offsets
  • Need sub-pixel precision
  • Complex layouts with overlapping elements

6. Production Framework Integration

Page Object with Dimensions

public class LoginPage {
private WebElement submitButton;

public LoginPage(WebDriver driver) {
submitButton = driver.findElement(By.id("submit-btn"));
}

public void verifyButtonSize() {
Rectangle rect = submitButton.getRect();
assertTrue("Button too small", rect.getWidth() >= 100 && rect.getHeight() >= 30);
}

public Point getButtonPosition() {
return submitButton.getRect().getPoint(); // Top-left corner
}
}

TestNG Assertions

@Test
public void testButtonDimensions() {
Rectangle rect = button.getRect();

// Soft assertions for responsive design
SoftAssert softAssert = new SoftAssert();
softAssert.assertTrue(rect.getWidth() > 80, "Button too narrow");
softAssert.assertTrue(rect.getHeight() > 25, "Button too short");
softAssert.assertAll();
}

Method Comparison Table

MethodSelenium VersionReturnsPosition?Size?Best For
getRect()4+Rectangle✅ x,y✅ w,hModern apps
getSize()3+Dimension✅ w,hLegacy
getLocation()3+Point✅ x,yLegacy
getBoundingClientRect()AllMap/JS ObjectComplex CSS

Visual Debugging (Screenshot + Dimensions)

public void debugElement(WebElement element, String name) {
Rectangle rect = element.getRect();

// Screenshot with overlay
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

// Log dimensions
System.out.println(name + ": " + rect.getX() + "," + rect.getY() +
" " + rect.getWidth() + "x" + rect.getHeight());

// Save screenshot
FileUtils.copyFile(screenshot, new File("debug/" + name + ".png"));
}

Best Practices

  1. Use getRect() - Selenium 4 single method for everything
  2. Viewport aware - Test on different screen sizes
  3. Tolerance in assertions - Responsive designs vary ±10px
  4. Combine with screenshots - Visual proof of layout issues
  5. Log dimensions - Essential for debugging responsive failures

getRect() is the modern standard - replaces two legacy methods with one.