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
- Locate the pre-filled text field by its id and store it in a variable.
- Ask for the
value,nameandtypeattributes, one call each, reusing the variable. - Join each answer to its label with
+. Each answer is already a string here, so no conversion is needed. - 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()returnsNone, and adding it to a string raisesTypeErrorwith the messagecan only concatenate str (not "NoneType") to str. Wrap it instr()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 getNone, 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.
getAttributeraisesAttributeErrorbecause the element has no such member. The Python name isget_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.
Related lessons
- Clear a Field and Type AgainUses the same pre-filled field and reads its
valuebefore and after clearing it. - Read Visible TextReads visible text, which is different from an attribute.
- Tell a Disabled Checkbox from an Enabled OneReads the
disabledattribute on a checkbox and handles its string result.
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.
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()