The getSize() method in Selenium WebDriver is used to retrieve the dimensions of a web element. This method returns the width and height of the element in pixels. Understanding how to use the getSize() method is important for verifying the size of elements within a web application, which is essential for layout testing and ensuring that elements are displayed correctly across different devices and browsers.


// Assuming 'element' is an instance of WebElement
Dimension size = element.getSize();


  1. Getting the Size of a Web Element:
WebElement element = driver.findElement("elementId")); 
Dimension size = element.getSize(); 
int width = size.getWidth(); 
int height = size.getHeight();


import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class GetSizeExample {
    public static void main(String[] args) {
        // Set path to the ChromeDriver executable
        System.setProperty("", "path/to/chromedriver");

        // Initialize ChromeDriver
        WebDriver driver = new ChromeDriver();

        // Open a webpage

        // Locate an element using its ID and get its size
        WebElement element = driver.findElement("submit"));
        Dimension size = element.getSize();

        // Print the width and height of the element
        System.out.println("Submit button width: " + size.getWidth());
        System.out.println("Submit button height: " + size.getHeight());

        // Close the browser


  • Layout Verification: The getSize() method is crucial for verifying the dimensions of elements on a webpage, ensuring that elements are rendered with the correct size.
  • Responsive Design Testing: It aids in testing responsive designs by allowing testers to check if elements adapt their sizes correctly across different screen sizes and devices.
  • UI Consistency: Helps maintain UI consistency by ensuring that elements have the expected dimensions, which is important for a seamless user experience.


  • Dynamic Content: If the page content changes dynamically, the size of elements may also change. Ensure that elements are located and their sizes are checked in the correct context.
  • CSS Influence: The size retrieved by getSize() can be influenced by CSS properties such as padding, border, and margin, which may need to be considered when verifying element dimensions.


The getSize() method in Selenium WebDriver is a valuable tool for retrieving the dimensions of web elements. It is essential for verifying the layout and ensuring that elements are displayed with the correct size, which is crucial for UI testing and responsive design validation. By mastering the use of the getSize() method, testers can enhance their ability to create precise and reliable automated tests that accurately reflect the user experience and ensure consistency across different devices and browsers.

Was this article helpful?
Leave a Reply 0

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