Get attribute function in Selenium Webdriver

asked 3 months ago

What is attribute:

The attributes are the key-value pairs in the HTML Tag.

Sometimes we need to get the attribute to verify the properties and the values of the web element.
The examples are,  fetching href value of the hyperlink, title, style, and so on

Example: Navigate to https://www.qafeast.com/demo and click the Button tab on the left side. 

 

 

The button Submit HTML properties,

button id="button-1" class="btn btn-success btn-lg" data-toggle="tooltip" title="Click to submit the button">Submit

The attributes are id, class, id, data-toggle, to get the value of the title = "Click to submit the button" we can use getAttribute() function.

 

Example:

 

String strAttribute = driver.findElement(By.id("button-1")).getAttribute("title");
System.out.println("Attribute value is - "+strAttribute);

 

Output: Attribute value is - Click to submit the button