The getSize() method is used to get the size of a web element in Selenium. This post will discuss the getSize() method of the WebElement interface in detail.
- Method declaration – Dimension getSize();
- What does it do? It will get the size of the element on the web page.
- What does it return? getSize() method returns a Dimension class object. Dimension class provides getHeight() and getWidth() methods to get the WebElement’s height and width.
Code Example
We will use the getSize() method on the below-highlighted element, which will return a Dimension object. Then we will use its getHeight() and getWidth() method to find the element’s height and width. You can find the highlighted element on this page – https://testkru.com/Elements/TextFields.
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
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"));
Dimension dimension = element.getSize();
System.out.println("Height of the element: " + dimension.getHeight());
System.out.println("Width of the element: " + dimension.getWidth());
}
}
Output –
Height of the element: 37
Width of the element: 241
getSize() and getRect() methods
We can also get the size of an element using the getRect() method. getRect() method returns a Rectangle class object which has a getDimension() method to get the dimension of a web element.
So, we can either use the getSize() method or the getRect().getDimension() method to get the size of an element on a web page.
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"));
Dimension dimension = element.getRect().getDimension();
System.out.println("Height of the element: " + dimension.getHeight());
System.out.println("Width of the element: " + dimension.getWidth());
}
}
Output –
Height of the element: 37
Width of the element: 241
What if we used the getSize() method on a null WebElement?
We will get a NullPointerException as illustrated by the below program.
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 = null;
Dimension dimension = element.getSize();
System.out.println("Height of the element: " + dimension.getHeight());
System.out.println("Width of the element: " + dimension.getWidth());
}
}
Output –
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebElement.getSize()" because "element" is null
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.