Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

getAttribute() method in Selenium WebDriver – Why, What and How to use?

Posted on 03/21/2025 By admin

Selenium WebDriver provides an important method named “getAttribute(String attributeName)“. In this post, we will learn below topics:-

  1. What is getAttribute() method?
  2. Why we use getAttribute() method?
  3. How to use getAttribute() method?

We will see a lot of examples as well. Let’s start:-

A web developer defines attribute and properties to a web element to add extra meaning to it. For example: I am a human and I have a name, height, length, color etc which are attributes and properties of mine.

There are slight difference between attribute and properties of web element.

  1. Attributes carry additional information about an HTML element and come in name="value" pairs. Example: . Here we have a div tag and it has a class attribute with a value of my-class. Property is a representation of an attribute in the HTML DOM ( Document Object Model ) tree. So the attribute in the example above would have a property named className with a value of my-class.
  2. Attributes are defined by HTML while Properties are defined by DOM.
  3. Some HTML attributes have 1:1 mapping onto properties. id is one example of such and some do not. For example: the value attribute specifies the initial value of an input, but the value property specifies the current value.
  4. Attributes are in your HTML text document/file, whereas properties are in HTML DOM tree. This means that attributes do not change and always carry initial (default) values. However, HTML properties can change, for example when user checks a checkbox, inputs text to text area or uses JavaScript to change the property value.

For more details check this post.

So, during development of automation script, we need to retrieve values of attributes or properties of web element to verify check points. For an example :- Consider a movie ticket booking application. Color of available seat will be different from booked one. Available seat color will be green and booked seat color will be red. So, to check whether seat is available or booked, we may need to fetch attribute (It may be color or back ground color) value through script and based on value we need to perform assertions or further actions.

So to achieve this Selenium WebDriver provides a method called getAttribute().

Syntax: java.lang.String getAttribute(java.lang.String name)

getAttribute() is method which is declared in WebElement interface.

It returns the current value of the given attribute as a String of the Web element. For example:- If we pass “class” as an attribute to getAttribute() method, it will return the value of “class” attribute. E.g. String classValue = ele.getAttribute(“class”)

I have used CURRENT word above. Actually value of attribute may change as well. So it will always return current value of attribute. When we call getAttribute() method for an attribute, it always looks for property with same name. If it finds property, it return the current value of it. If property is not found then it looks for attribute and returns value. If neither is found, it returns null.

As per Javadoc of Selenium WebDriver:-

“Get the value of the given attribute of the element. Will return the current value, even if this has been modified after the page has been loaded. More exactly, this method will return the value of the property with the given name, if it exists. If it does not, then the value of the attribute with the given name is returned. If neither exists, null is returned.”

There are another two important points about this method:-

  1. getAttribute() method will return either “true” or null for attributes whose value is Boolean. E.g. async, autofocus, autoplay, checked etc.
  2. The following commonly mis-capitalized attribute/property names are evaluated as expected: If the given name is “class”, the “className” property is returned. If the given name is “readonly”, the “readOnly” property is returned.

getAttribute() method work on specific web element. So first you need to locate a web element and then you need to call getAttribute() method on it by specifying attribute for which value you require.

We will see examples now:

HTML code for search box of Google:-


Above web element (input tag) has many attributes. For e.g. maxlength, name, autocomplete etc. We can retrieve values for these attributes using getAttribute() method.

public class GetAttributeExamples {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.google.com");
        WebElement searchTextBox = driver.findElement(By.id("lst-ib"));
        // retrieving html attribute value using getAttribute() method
        String titleValue = searchTextBox.getAttribute("title");
        System.out.println("Value of title attribute: " + titleValue);
        String autocompleteValue = searchTextBox.getAttribute("autocomplete");
        System.out.println("Value of autocomplete attribute: " + autocompleteValue);
        // Retrieving value of attribute which does not exist
        String nonExistingAttributeValue = searchTextBox.getAttribute("nonExistingAttribute");
        System.out.println("Value of nonExistingAttribute attribute: " + nonExistingAttributeValue);
    }
}

Output:Value of title attribute: SearchValue of autocomplete attribute: off

Value of nonExistingAttribute attribute: null

Uncategorized

Post navigation

Previous Post: patch
Next Post: Advanced TestNG Tutorials 34: How To Pass a Group Name to be Run at Runtime in TestNG XML Using Beanshell

Related Posts

image – Make Selenium Easy Uncategorized
API Testing – Postman Uncategorized
Git Tutorial 2 – What is GIT and Its Advantages? Uncategorized
June 10, 2018 – Make Selenium Easy Uncategorized
Interview for Automation Testing Profile Uncategorized
Git Tutorial 32 – How To Restore Deleted And Committed But Not Pushed File Without GIT Reset? Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark