Implementation:SeleniumHQ Selenium Rectangle
| Knowledge Sources | |
|---|---|
| Domains | WebDriver, Data_Model |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete value object for representing a rectangular region with position and size provided by the Selenium WebDriver library.
Description
Rectangle is an immutable value object that combines position (x, y) and size (width, height) into a single representation of a rectangular area. It can be constructed either from individual integer coordinates or from existing Point and Dimension objects. The class provides getter methods for all four fields, convenience methods to extract the position as a Point or the size as a Dimension, and a private toJson() method for serialization. It correctly implements equals() and hashCode() for value semantics.
Usage
Use Rectangle when you need to represent the full bounding box of a web element, combining both its location and dimensions. It is returned by WebElement.getRect() and is the standard way to obtain the complete geometry of an element in a single call.
Code Reference
Source Location
- Repository: SeleniumHQ_Selenium
- File: java/src/org/openqa/selenium/Rectangle.java
Signature
public class Rectangle {
public final int x;
public final int y;
public final int height;
public final int width;
public Rectangle(int x, int y, int height, int width)
public Rectangle(Point p, Dimension d)
public int getX()
public int getY()
public int getHeight()
public int getWidth()
public Point getPoint()
public Dimension getDimension()
@Override public boolean equals(Object o)
@Override public int hashCode()
}
Import
import org.openqa.selenium.Rectangle;
I/O Contract
| Constructor | Parameters | Description |
|---|---|---|
Rectangle(int, int, int, int) |
x, y, height, width | Creates a rectangle from raw coordinates and size |
Rectangle(Point, Dimension) |
Point p, Dimension d | Creates a rectangle from a Point (position) and Dimension (size) |
| Method | Return Type | Description |
|---|---|---|
getX() |
int |
Returns the x coordinate of the top-left corner |
getY() |
int |
Returns the y coordinate of the top-left corner |
getHeight() |
int |
Returns the height of the rectangle |
getWidth() |
int |
Returns the width of the rectangle |
getPoint() |
Point |
Returns the position as a Point object |
getDimension() |
Dimension |
Returns the size as a Dimension object |
equals(Object) |
boolean |
Returns true if x, y, height, and width all match |
hashCode() |
int |
Hash based on x, y, height, and width |
Usage Examples
// Create a Rectangle from raw values
Rectangle rect = new Rectangle(10, 20, 300, 400);
// Create a Rectangle from Point and Dimension
Point position = new Point(10, 20);
Dimension size = new Dimension(400, 300);
Rectangle rect2 = new Rectangle(position, size);
// Retrieve the full bounding box of a web element
WebElement element = driver.findElement(By.id("content"));
Rectangle bounds = element.getRect();
int x = bounds.getX();
int y = bounds.getY();
int w = bounds.getWidth();
int h = bounds.getHeight();
// Extract position and size as separate objects
Point topLeft = bounds.getPoint();
Dimension dim = bounds.getDimension();
// Compare two Rectangle objects
Rectangle a = new Rectangle(0, 0, 100, 200);
Rectangle b = new Rectangle(0, 0, 100, 200);
boolean same = a.equals(b); // true