logo image

getLocation(), getSize(), getTagName() and getText() WebElement methods in selenium webdriver



In this tutorial i am going to explain following important methods in selenium webdriver. These methods are used to get information about a particular element

  1. getLocation() : Element is where on the page.
  2. getSize() : It Returns width and height of the element.
  3. getTagName() : It gets the tag name of the current element.
  4. getText() : It gets visible text for an element.

Example :

package com.selenium.practise;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class ExampleWebElementMethods {

	public static void main(String[] args) {
		System.setProperty("webdriver.chrome.driver", "E:\\Soft Wares\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		String Url = "https://google.com";
		driver.get(Url);
		WebElement sigin = driver.findElement(By.id("gb_70"));
		//getLocation() WebElement method
		Point location = sigin.getLocation();
		System.out.println("Location of X :"+ location.getX()+ "," +"Location of Y :" +location.getY());
		//getSize() WebElement method
		Dimension size = sigin.getSize();
		System.out.println("Width :"+size.getWidth()+ "," +"Height : "+size.getHeight());
		//getText() WebElement method
		String text = sigin.getText();
		System.out.println("Text :" +text);
		//getTagName() WebElement method
		String tagName = sigin.getTagName();
		System.out.println("Tagname :"+tagName);
		//close the browser
		driver.close();

	}

}

Output : Location of X :934,Location of Y :16 Width :69,Height : 30 Text :Sign in Tagname :a