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
- Call
getAttribute()on the field with"value"and print the result after the labelValue:. - Do the same with
"name"and the labelName:. - Do the same with
"type"and the labelType:. - Find the field with the id
disabledField, callgetAttribute("disabled")on it, and print the result after the labelDisabled:. It printstrue.
Common mistakes
Passing the wrong attribute name
Selenium does not throw for a misspelled name; you get
nullor 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, asplaceholderdoes on this field.Expecting "false"
An absent boolean attribute gives
null. Comparing the result with "false" never matches, and callingequals()on thenullthrowsNullPointerException.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 usecontains().
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().
Related lessons
- CSS Selectors - Attribute SelectorsFind elements by the same attributes you read here.
- Get CSS Value with getCssValue()Read computed styles, which attributes do not show.
- Find Element by Link TextA link's
hrefis a common attribute check; see how it differs from the HTML.
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.
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();
}
}