My Thoughts: Selenium WebDriver : getSize() Vs getLocation() functions

Friday, May 25

Selenium WebDriver : getSize() Vs getLocation() functions

Here are the difference between getSize() and getLocation() methods :

getSize() : 
  1. It will returns the "Dimension" object.
  2. If you want to get the width and Height of the specific element on the webpage then use "getsize()" method.
Sample code to get the width and height :
WebDriver driver = new FirefoxDriver(); 
driver.get("https://google.com");
Dimension dimesions=driver.findElement(By.id("gbqfsa")).getSize();
System.out.println("Width : "+dimesions.width);
System.out.println("Height : "+dimesions.height);
Note:
Makesure to import "org.openqa.selenium.Dimension;" package.

getLocation() :
  1. It will return the "Point" object.
  2. If you want to get the exact "x" and "y" coordinates of the element then use "getLocation()"  method.

Sample code to get the "x" and "y" coordinates :
WebDriver driver = new FirefoxDriver(); 
driver.get("https://google.com");
Point point=driver.findElement(By.id("gbqfsa")).getLocation();
System.out.println("X Position : "point.x);
System.out.println("Y Position : "point.y);
Note:
Makesure to import "org.openqa.selenium.Point;" package.

1 comment: