selenium tutorial testing tools

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 = By.name("btnK");
							
	@BeforeClass
	public void setUp() {
		driver = new FirefoxDriver();
		driver.get("http://www.google.com");
	}

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

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

	@Test
	public void getAttribute_class() {

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

	}

	@Test
	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"));
	}
	
	@Test
	public void getAttribute_ButtonLabel() {

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

	@AfterClass
	public void tearDown() {
		driver.quit();
	}
}

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: 

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Wanted Contributers

Hello Viewer! We're a relatively a small group of writers who have combined efforts to write the articles. Well, we need more help from talented people to contribute and grow the size of articles. It's great work if you're looking for a little money on the side – make this as your Part time job.

We are mainly looking into Protractor, Cucumber, JBehave, RSpec, Behat Mink, Selenium Python or any other tool which uses selenium.

If you can contribute on any the above, Please contact us.