Get Attribute values using Webdriver

There are cases where you want to get the attributes values and then perform any action.

In the below, if you see, button tag which has multiple attributes 'name', 'id', 'class' and 'aria-label' and has values for each attribute. To get the attribute value using selenium webdriver, we can use 'element.getAttribute(attributeName)'.

If we try to get the attribute value that doesn't exists for the tag, it will return null value.

<button name="btnK" id="gbqfba" aria-label="Google Search" class="gbqfba"><span id="gbqfsa">Google Search</span></button>

Let us see the examples to get attributes for a 'Google Search' button. Above is the HTML for button tag.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class GetAttributes {

	public WebDriver driver;
	private By bySearchButton ="btnK");
	public void setUp() {
		driver = new FirefoxDriver();

	public void getAttribute_ButtonName() {
		WebElement googleSearchBtn = driver.findElement(bySearchButton);
		System.out.println("Name of the button is:- " +googleSearchBtn.getAttribute("name"));

	public void getAttribute_Id() {
		WebElement googleSearchBtn = driver.findElement(bySearchButton);
		System.out.println("Id of the button is:- "+ googleSearchBtn.getAttribute("id"));

	public void getAttribute_class() {

		WebElement googleSearchBtn = driver.findElement(bySearchButton);
		System.out.println("Class of the button is:- "+ googleSearchBtn.getAttribute("class"));


	public void getAttribute_InvalidAttribute() {

		WebElement googleSearchBtn = driver.findElement(bySearchButton);
		//Will return null value as the 'status' attribute doesn't exists
		System.out.println("Invalid Attribute status of the button is:- "+ googleSearchBtn.getAttribute("status"));
	public void getAttribute_ButtonLabel() {

		WebElement googleSearchBtn = driver.findElement(bySearchButton);
		System.out.println("Label of the button is:- "+ googleSearchBtn.getAttribute("aria-label"));

	public void tearDown() {

After executing the above code the output should look something like below:

Output :

Label of the button is: Google Search
Name of the button is:- btnK
Id of the button is:- gbqfba
Invalid Attribute status of the button is: null
Class of the button is: gbqfba
PASSED: getAttribute_ButtonLabel
PASSED: getAttribute_ButtonName
PASSED: getAttribute_Id
PASSED: getAttribute_InvalidAttribute
PASSED: getAttribute_class

Selenium Tutorials: 


How to save getAttribute("value") text in string after end of the test?

what if you have no aria-label atributa and you want to know what is the text of button.
Other words, what if you have only <span id="gbqfsa">Google Search</span>

The solution for this is to use element.getText() instead of element.getAttribute(attributeName)

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.