Get the size and location coordinates of a webelement using Selenium 4 and JAVA

Versions used while doing this blog post, (These are the prerequisites to automate chrome browser. Browser and driver versions should match)
selenium-java version – 4.4.0
chrome browser version – 105
chrome driver version – 105 (Download link : https://chromedriver.chromium.org/downloads) [Using WebDriverManager to auto download the expected version]

Size and location coordinates of a webelement can be captured using selenium 4 by following below steps:
1. Map chromedriver using WebDriverManager,

WebDriverManager.chromedriver().setup();

2. Instantiate (creation of object) driver object with ChromeDriver() class (This step opens the chrome browser)

ChromeDriver driver = new ChromeDriver(); 

3. Now, next steps of maximizing the browser and setting implicit timeout for driver object

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
driver.manage().window().maximize();

4. Launch the expected website URL and observe the offline scenario

driver.get("https://www.google.com");

5. Add an explicit wait that waits till the expected element appears [Note : WebDriverWait(driver object, Duration method) can be used as WebDriverWait(driver object, long) got deprecated now]

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
WebElement logo = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("hplogo")));

6. Get the size and location of the element using getRect() method [Note : As per selenium changelog, Replaced WebElement.getSize() and WebElement.getLocation() with a single method WebElement.getRect()]

Rectangle logoSizeAndLocations = logo.getRect();
System.out.println("Height = "+logoSizeAndLocations.height);
System.out.println("Width = "+logoSizeAndLocations.width);
System.out.println("X coordinate = "+logoSizeAndLocations.x);
System.out.println("Y coordinate = "+logoSizeAndLocations.y);

Sample code for reference

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;

public class GetSizeAndLocationOfElementUsingSelenium4 {

    public static void main(String[] args) {
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
        driver.manage().window().maximize();
        driver.get("https://www.google.com");

        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
        WebElement logo = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("img[alt='Google']")));

        Rectangle logoSizeAndLocations = logo.getRect();
        System.out.println("Height = "+logoSizeAndLocations.height);
        System.out.println("Width = "+logoSizeAndLocations.width);
        System.out.println("X coordinate = "+logoSizeAndLocations.x);
        System.out.println("Y coordinate = "+logoSizeAndLocations.y);

        driver.quit();
    }
}

Leave a Reply

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