getRect() method in Selenium Java

The getRect() method is used to get the location and the size of the web element. This post will discuss the getRect() method of the WebElement interface in detail.

  • Method declaration – Rectangle getRect();
  • What does it do? It finds the rectangle associated with the specified web element. If we have a rectangle, we can find the size and location of the element as well, which we will look at shortly.
  • What does it return? It returns a Rectangle class object, and with it comes the methods to find the web element’s location and size.

Rectangle class provides us with the below getter methods –

  • getDimension()
  • getPoint()
  • getHeight()
  • getWidht()
  • getX()
  • getY()
Code Example

We will find the height and width of the highlighted element using the getRect() method. We will use the 1st text field on this page – https://testkru.com/Elements/TextFields.

Text field element
import org.openqa.selenium.By;
import org.openqa.selenium.Rectangle;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/TextFields");

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

		Rectangle rect = element.getRect();

		System.out.println("Height of element: " + rect.getHeight());
		System.out.println("Width of element: " + rect.getWidth());

	}
}

Output –

Height of element: 37
Width of element: 241
getLocation() and getRectangle() methods
  • We had earlier discussed the getLocation() method of the WebElement interface, which returns a Point object, and the getRect() method returns a Rectangle class’s object.
  • Rectangle class provides a getPoint() method to retrieve the corresponding Point object.
  • We will get the same location using getLocation() or the getRect() method.
  • Rectangle class has getX() and getY() methods, whereas the Point class also has the same ones. Using getX() and getY() methods of the Rectangle or the Point class would yield the same result. This is also illustrated in the below example.
public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/TextFields");

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

		Point point = element.getLocation();
		Rectangle rect = element.getRect();

		System.out.println("X location using point object: " + point.getX());
		System.out.println("X location using rectangle object: " + rect.getX());

		System.out.println("Y location using point object: " + point.getY());
		System.out.println("Y location using rectangle object: " + rect.getY());

	}
}

Object –

X location using point object: 532
X location using rectangle object: 532
Y location using point object: 427
Y location using rectangle object: 427
How to get the area of a web element?

Rectangle class provides the getHeight() and getWidth() methods to get the web element’s height and width. We can multiply them to get the area of the web element.

public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\MEHUL\\OneDrive\\Desktop\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/TextFields");

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

		Rectangle rect = element.getRect();

		int height = rect.getHeight();
		int width = rect.getWidth();

		System.out.println("Height of the web element: " + height);
		System.out.println("Width of the web element: " + width);
		System.out.println("Area of the web element: " + (height * width));
	}
}

Output –

Height of the web element: 37
Width of the web element: 241
Area of the web element: 8917

This is it. Please visit this link to learn more about WebElement and its methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *