Difference between getText() and getAttribute() method in Selenium WebDriver

In this tutorial, we will discuss about GetText() method as well as getAttribute() method in Selenium WebDriver. These methods used extensively in automating a web application. Before going through these methods, we should know what HTML attributes are.

What are HTML attributes?

Attributes are the additional information provided by developers in HTML tags. Attributes are normally defined using “name-pair” values. Let us see, here div is the tag and corresponding attribute with property name is id and property value is nav-xshop.
<div id= “nav-xshop”>


What is getAttribute() method?

The getAttribute() method is declared in the WebElement interface, and it returns the value of the web element’s attribute as a string. It fetches the value of an attribute, in HTML code whatever is present in the left side of ‘=’ is an attribute, value on the right side is an attribute value.

  • getAttibute() returns ‘null’ if there no such attribute
  • getAttribute() method will return either “true” or null for attributes whose value is Boolean.

Where to use getAttribute() method?

Consider a scenario of movie ticket booking. The color of booked and available seats in movie theatre are different. So same seat will have suppose white color for available seat and blue for booked seat. So, the color attribute of seat changes from white to blue for same seat which can be identified by getAttribute() method. The snippet below shows the HTML code of search box of Amazon Home page

Below is an example on how getAttribute() method can be used.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class GetAttributeDemo {
      public static void main(String[] args) {
 
            System.setProperty("webdriver.chrome.driver",
                                                "C:\\Users\\Vibha\\Desktop\\Drivers\\chromedriver_win32\\chromedriver.exe");
 
             WebDriver driver = new ChromeDriver();
 
             driver.get("https://www.amazon.com/");
             driver.manage().window().maximize();
 
             WebElement AmazonSearchBox = driver.findElement(By.name("field-keywords"));
             System.out.println("Name of the Email Textbox is:- " + AmazonSearchBox.getAttribute("name"));
 
             System.out.println("Class of the Email Textbox is:- " + AmazonSearchBox.getAttribute("class"));
 
             System.out.println("Value of the Email Textbox is:- " + AmazonSearchBox.getAttribute("tabindex"));
 
             System.out.println("Type of the Email Textbox is:- " + AmazonSearchBox.getAttribute("type"));
 
             System.out.println("Id of the Email Textbox is:- " + AmazonSearchBox.getAttribute("id"));
 
             // getAttibute() returns 'null' if there no such attribute
 
             System.out.println("Value of nonExistingAttribute is:- " + AmazonSearchBox.getAttribute("test"));
 
            driver.close();
       }
 
}

Output
Name of the Email Textbox is:- field-keywords
Class of the Email Textbox is:- nav-input
Value of the Email Textbox is:- 19
Type of the Email Textbox is:- text
Id of the Email Textbox is:- twotabsearchtextbox
Value of nonExistingAttribute is:- null

What is getText() method?

getText() is a method which gets us the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing white space.

Inner text is text between the opening tags and closing tags.

This is the example of getText

In above example, texts  “This is the example of getText” between opening and closing tags of h1 are called inner text.

The snippet below shows the HTML code of search box of Amazon Home page

Below is an example on how getText() method can be used.

import java.util.concurrent.TimeUnit;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class GetTextDemo {
 
      public static void main(String[] args) {
           System.setProperty("webdriver.chrome.driver",
                                                "C:\\Users\\Vibha\\Desktop\\Drivers\\chromedriver_win32\\chromedriver.exe");
 
            WebDriver driver = new ChromeDriver();
 
            driver.get("https://www.facebook.com/");
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 
            String FacebookText = driver
                                    .findElement(By.xpath("//*[@id='content']/div/div/div/div/div[2]/div/div[1]/div[1]/span")).getText();
            System.out.println("Text on Facebook Site:- " + FacebookText);
         driver.close();
      }
 
}

Output
Text on Facebook Site:- Create an account

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s