Learn about new getRect(), getDimension() methods in Selenium 4 to easily fetch element size and location

New getRect(), getDimension()​​ methods​​ in Selenium 4 to easily fetch element size and location

In Selenium 3, we have​​ a separate method​​ to get location of an element​​ getLocation()​​ and a separate method to get size of an element​​ getSize()

Selenium 4 has introduced a new method​​ getRect()​​ that combines both the above functionalities.

Let us see this in practice.​​ Go to the site​​ https://www.way2automation.com/lifetime-membership-club/​​ 

A link​​ ‘Member Login’​​ would be seen​​ 

Click​​ this​​ link, below page comes up

Let us find the location (x and y co-ordinates) along with size (height and width) of ‘Email’ text field that we see above.

Let us inspect this field

So we can write

Next,​​ as can be seen below, there is getRect() method that can be invoked on this element

So we have

This method returns an object of Rectangle

Import Rectangle from org.opena.selenium viz​​ 

Next, we can now invoke​​ getHeight(), getWidth(), getX(), getY()​​ methods on this object

So we can write

Let us run the script, notice below that respective details are printed

Let us try to fetch the same details using selenium 3 methods. This is to ensure that there is no difference in results between selenium 3 and selenium 4 methods:​​ 

Run the script.

Notice​​ the console output.

Selenium 3 methods too are printing the same results​​ 

A new method​​ getDimension()​​ is also being introduced. This will get us the (Width, Height) of an element

Save and run the script.

Notice below that (Width, Height) of an element is printed​​ 

Let us look at one more example and fetch the details of below logo

Let us write the below lines

Save and run

Notice that the​​ height and width do match with the details seen on the html page, see below

Code snippet

package sel4scripts;

 

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 io.github.bonigarcia.wdm.WebDriverManager;

 

public class ElementSizeLocation {

 

public static void main(String[] args) {

  WebDriverManager.chromedriver().setup();  

  WebDriver driver = new ChromeDriver();  

  

  //driver.get("https://sso.teachable.com/secure/673/identity/login");

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

    

  //WebElement elem = driver.findElement(By.id("email"));

  WebElement elem = driver.findElement(By.cssSelector(".custom-logo"));

  

    //Selenium 4

    Rectangle r = elem.getRect();

    

    System.out.println("Height of element is-->" + r.getHeight());

    System.out.println("Width of element is-->" + r.getWidth());

    System.out.println("X co-ordinate of element is-->" + r.getX());

    System.out.println("Y co-ordinate of element is-->" + r.getY());

    System.out.println("Dimension of element-->" + r.getDimension());

    

    System.out.println("==============================================");

    

    //Selenium 3

    System.out.println("Height Selenium 3 -->" + elem.getSize().getHeight());

    System.out.println("Width Selenium 3 -->" + elem.getSize().getWidth());

    System.out.println("X co-ordinate Selenium 3 -->" + elem.getLocation().getX());

    System.out.println("Y co-ordinate Selenium 3 -->" + elem.getLocation().getY());

}

 

}

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

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

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 31st DEC 2024