Skip to content
CodeKruPro

Get Attribute Value with getAttribute()

20/70

Video walkthrough

Watch on YouTube

Get Attribute Value

getAttribute() reads any HTML attribute value. Common attributes: value, name, id, class, placeholder, type, href.

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Find the pre-filled field (ID: preFilledTextField)
  3. Print its value, name, and type attributes
  4. Find the disabled field (ID: disabledField) and print its disabled attribute

Some attributes are only a name, with no value after them. disabled is one of them:

<input type="text" id="disabledField" value="Codekru" disabled>

For an attribute like this, getAttribute() returns "true" when it is on the tag, and null when it is not.

Expected output:

Value: Codekru
Name: preFilledTextField
Type: text
Disabled: true
Read full tutorial on Codekru

About this lesson

getAttribute in Selenium Java: read value, name, type and other attributes

Selenium Java AssignmentsBeginnerWeb Element Interactions

How it works

getAttribute() takes a name and returns an element's attribute or property as a String, or null when there is neither. It is not deprecated in Selenium 4. It checks the live DOM property first and falls back to the HTML attribute, so for value it gives what is in the field now, including text a user or a test has typed.

Selenium 4 also has two narrower methods. getDomAttribute() returns only what is written in the HTML, and getDomProperty() returns only the JavaScript property. The difference shows on Text Fields: a textarea has no type attribute, so getDomAttribute("type") is null, but getAttribute("type") returns the property, which is "textarea".

Boolean attributes come back as the string "true" when present and null when absent. On this page getAttribute("disabled") is null for the first-name field and "true" for the disabled one, which is why you should test for null, not for "false".

About the assignment

The starter hands you the pre-filled field. You will read three attributes from it, each answering a different question: what it holds (value), what the form sends it as (name), and what kind of input it is (type). Then you read disabled from the disabled field, an attribute that is only a name with no value.

What you will do

  1. Call getAttribute() on the field with "value" and print the result after the label Value:.
  2. Do the same with "name" and the label Name:.
  3. Do the same with "type" and the label Type:.
  4. Find the field with the id disabledField, call getAttribute("disabled") on it, and print the result after the label Disabled:. It prints true.

Common mistakes

  • Passing the wrong attribute name

    Selenium does not throw for a misspelled name; you get null or an empty string, and the mistake only shows up in the output. The empty string happens when the name happens to match a real property, as placeholder does on this field.

  • Expecting "false"

    An absent boolean attribute gives null. Comparing the result with "false" never matches, and calling equals() on the null throws NullPointerException.

  • Reading class and expecting one name

    getAttribute("class") returns the whole list, "pt-1 pb-1 pr-2 pl-2" on this field. To test for one class, split the string or use contains().

In a real test suite

Attribute checks verify state that has no visible text: a link's href, an image's alt, aria-expanded on a menu, aria-invalid on a form field after validation, or a data- attribute the app sets when something finishes.

Lesson 19 read visible text. Attributes tell you what the HTML says; lesson 21 asks what the browser actually computed for an element's styling, with getCssValue().

To try one of these calls on any page without a task, open the online Selenium compiler for Java.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/TextFields");
        
        WebElement field = driver.findElement(By.id("preFilledTextField"));
        
        // TODO: Print its value, name, and type attributes
        
        // TODO: Find the disabled field (id: disabledField) and print its disabled attribute
        
        driver.quit();
    }
}
Read the full tutorial