Difference b/w getText() and getAttribute() in Selenium WebDriver


Selenium WebDriver is an automation tool that is used to automate the testing of web applications and make sure they work as expected. Automation means the programmer doesn't have to write testing scripts; Selenium can write test cases without any script.

Selenium supports a wide variety of programming languages such as Java, Python, PHP, Ruby, C#, Perl, Scala, etc. which means Selenium can provide test cases in any of these languages. It supports all the popular browsers such as Chrome, Firefox, Safari, and Internet Explorer. Selenium is an open-source tool which makes it even more popular among developers.

In this article, we will specifically cover the differences between getText() and getAttribute() methods in Selenium WebDriver. But, before that let's get a quick introduction to HTML tags and attributes. HTML tags mark the starting and ending of HTML elements and HTML attributes are used to define the characteristics of an HTML element and are placed inside the element's opening tag. All attributes are made up of two parts - a name and a value. For example,

<p align = "left">This is left aligned

Here, <p> is the tag that holds the element and align="left" is its attribute.

Let's start with a brief example of what the getText() and getAttribute() methods would return and after that, we will discuss each of these methods in detail.

Take a look at the following HTML code −

<h2 style="font-size: 17px; text-transform:
uppercase;">Enjoy Premium Content at Affordable
Price

Here, style is an attribute whose value can be obtained by using the getAttribute() method. If we apply the getText() method on that element, we shall get the text.

The getText() Method

The getText() method returns the innerText of an element, that is the text which is visible on the page along with its sub-elements. Inner text is the text that is between the opening and closing tags. getText() ignores all the leading and trailing spaces.

Example

Let's take an example to understand how it works. First of all, we need to identify the element with help of any of the locators like id, class, name, xpath or CSS and then apply getText() method on it to get the text content of the element. Let us get the text of the element About Careers at Tutorials on the page −

Code Implementation with getText()

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class GetElementText{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver","C:\Users
      \ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
      driver.get("https://www.tutorialspoint.com/about/about_careers.htm");

      // identify element
      WebElement p=driver.findElement(By.xpath("//h1"));

      //getText() to obtain text
      String s= p.getText();
      System.out.println("Text content is : " + s);
      driver.close();
   }
}

It will produce the following output

The getAttribute() Method

The getAttribute() method fetches the text contained by an attribute in an HTML document. It returns the value of the HTML element's attribute as a string.

If a value is not set for an attribute, it will return a NULL value. For attributes with Boolean values, getAttribute() will return either "True" or NULL. The attribute is passed as a parameter to the method.

Example

Code Implementation with getAttribute().

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class GetAttributeMethd{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url ="https://www.tutorialspoint.com/videotutorials/subscription.php";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(5,
      TimeUnit.SECONDS);

      // identify element
      WebElement l = driver.findElements(By.cssSelector("h2"));

      // get style attribute with getAttribute()
      System.out.println("getAttribute() method:" + l.getAttribute("style"));
      driver.quit();
   }
}

It will produce the following output

Conclusion

To conclude, both the methods, getText() and getAttribute(), are used to retrieve data from an HTML element. The getText() method simply returns the visible text present between the start and end tags (which is not hidden by CSS). The getAttribute() method on the other hand identifies and fetches the key-value pairs of attributes within the HTML tags.

Updated on: 28-Jul-2022

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements