Getattribute in selenium java

getAttribute() method in Selenium: What, Why, and How to use

This short article will quickly explain everything a QA or a developer should know about the getAttribute method in Selenium. But before starting, let’s understand what are attributes in HTML web elements.

What are HTML Attributes?

Attributes are additional bits of information developers include in HTML tags. Attributes help in defining the characteristics of HTML elements. Apart from basic HTML tags like , , paragraph tag

, there are certain tags which can also include attributes.

Attributes are normally defined using “name-value” pairs. The name is the property that a developer wants to set. Let’s consider a basic HTML tag with an attribute title. It can be defined as follows:

In the above example, the h3 tag has an attribute with the property name as title and property value as HTML Attributes.

What is the 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. For attributes having boolean values, the getAttribute() method will return either true or null.

Why is the getAttribute() method required?

During the test script automation, QAs might need to fetch the attribute values of specific web elements to verify certain test scripts. Let’s understand this by taking an example.

Consider an air ticket booking application. The color of booked and available seats are different. Red represents the booked seats, and available seats are represented by green. So, for verifying whether a seat is booked or available, QAs need to fetch the attribute (color) value through the test script. Once the status of the seat is verified, only then can QAs verify further test scenarios.

Читайте также:  Regforum ru forum showthread php

How to use the getAttribute() method in Selenium?

The getAttribute() method in Selenium works on specific web elements. QAs need to locate the web elements first and then call the getAttribute() method by specifying the attributes for which values are required. One can quickly refer to this guide on locators in Selenium to understand how web elements can be located.

The snippet below represents the HTML code for the search box of duckduckgo.

The above Web Element has multiple attributes like class, type, name, etc.

Developers or QAs can retrieve values for these attributes using the getAttribute() method in Selenium.

Refer to the complete code below for better understanding:

public class GetAttributeSample < 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://duckduckgo.com/"); WebElement searchTextBox= driver.findElement(By.id("search_form_input_homepage")); // retrieving html attribute value using getAttribute() method String typeValue=searchTextBox.getAttribute("type"); System.out.println("Value of type attribute: "+typeValue); 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 type attribute: text
Value of autocomplete attribute: off
Value of nonExistingAttribute attribute: null

When the above code is executed, it automatically fetches the attributes – type and autocomplete. For the attribute which is not available, it returns the null value.

The getAttribute() method plays a vital role in automating decisive scenarios. Naturally, it becomes imperative for QAs to know how to use the getAttribute() method in Selenium.

It is best to test user scenarios on real devices and browsers before releasing any software. Here’s where teams can leverage BrowserStack’s real device cloud for performing automated Selenium tests on 2000+ browsers and real devices. By doing so, they can be assured that their code runs exactly as it is meant to in real user conditions.

Источник

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

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.
Читайте также:  Mockito kotlin data class

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().

What is getAttribute() method?

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.

How to use getAttribute() method?

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.

HTML code for search box of Google:-

Источник

Оцените статью