Skip to content
CodeKruPro

Read an Attribute

23/80

Read an Attribute

get_attribute reads an HTML attribute such as value, name, or type.

Task:

  1. Find #preFilledTextField
  2. Print its value, name, and type

Print the lines below exactly.

Expected output:

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

About this lesson

Using get_attribute in Selenium Python to read value, name and type

Selenium Python AssignmentsBeginnerWeb Element Interactions

How it works

An HTML attribute is a named piece of data inside a tag, such as the name and type of an input. get_attribute() takes the attribute name as a string and returns what the browser holds for it, as a Python str. You can call it on any element you have located, though some names get special handling, such as disabled returning true.

The result is None when the attribute is missing. On the pre-filled field the disabled attribute is absent, so asking for it gives None, not an empty string. Joining that result to a label raises TypeError with the message can only concatenate str (not "NoneType") to str.

Selenium 4 has two relatives. get_dom_attribute() returns only the DOM attribute, not the live property, while get_property() returns the live JavaScript property. For value they split apart once you type: after sending one letter, the live value had that letter at the end, but the written attribute still held the original word. get_attribute() tries the property first and falls back to the attribute, so it is the one that shows what a user sees.

About the assignment

Inspect one input element and report three facts about it: what it contains, what it is called, and what kind of control it is. All three are attributes of the same tag.

What you will do

  1. Locate the pre-filled text field by its id and store it in a variable.
  2. Ask for the value, name and type attributes, one call each, reusing the variable.
  3. Join each answer to its label with +. Each answer is already a string here, so no conversion is needed.
  4. Print the three lines in the order value, name, type.

Common mistakes

  • Joining a missing attribute to text

    If an attribute does not exist, get_attribute() returns None, and adding it to a string raises TypeError with the message can only concatenate str (not "NoneType") to str. Wrap it in str() or check it first when you are unsure it exists.

  • Typos in the attribute name

    Nothing complains about get_attribute("Name ") or a misspelled name. You just get None, though letter case alone does not matter. If a value is unexpectedly missing, compare the name with the page in DevTools.

  • Using the Java method name

    Python uses snake case. getAttribute raises AttributeError because the element has no such member. The Python name is get_attribute.

In a real test suite

Teams use attribute reads to check state that is not visible: a link target in href, a placeholder hint, or whether a field kept its value after a failed form submit. A small helper in conftest.py or a page object often wraps the call and a default.

The next lesson reads a different kind of information, the CSS style that the browser has computed.

To try one of these calls on any page without a task, open the Online Selenium Python Compiler.

Starter code

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

main.py
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
try:
    driver.get("https://testkru.com/Elements/TextFields")
    # Print value, name, and type of #preFilledTextField
finally:
    driver.quit()
Read the full tutorial