About this lesson
Selenium Python find_element By.NAME: find a form field and read its tag_name
Selenium Python AssignmentsBeginnerLocators
How it works
Form fields usually carry a name attribute, because the browser sends it to the server when the form is submitted. By.NAME searches that attribute. Names can repeat. Radio buttons in one group share a name on purpose.
Because names can repeat, find_element returns only the first match in page order, while find_elements gives you a list of all of them.
Two Python details show up here. element.tag_name is a property that returns the tag as a lowercase string, so you read it without brackets. And the value that a user would see inside a field is an attribute, so you read it with get_attribute("value"), not with .text, which is empty for an input. A locator that targets the wrong attribute fails with NoSuchElementException whenever the two attributes hold different text.
About the assignment
The Text Fields page has a read-only input whose name is given in the starter comment. You locate it through its name and report two facts about it: the text stored inside and the kind of tag it is.
What you will do
- Find the element using the name strategy and store it in a variable.
- Read the
valueattribute and print it after the labelValue:. - Read the tag of the same element from its property.
- Print the tag after the label
Tag:on the next line.
Common mistakes
Searching the name with By.ID
The name and the id are two separate attributes, and they only sometimes hold the same text. When they differ, searching a name through the id strategy raises
NoSuchElementException, so match the strategy to the attribute you actually have.Calling tag_name like a method
Writing
field.tag_name()raisesTypeError: 'str' object is not callable. It is a property that already holds astr.Expecting text from an input
Using
.texton the field gives an empty string. What you see inside is thevalueattribute.
In a real test suite
In suites that test forms, names are handy because they match the keys the application sends. A test helper can take a dict of field names and values, then loop over items() and fill each field in turn. That pattern is common with pytest parametrize as well.
The following lesson uses the class attribute instead, and that locator has a rule about spaces that trips up many beginners.
Related lessons
- Locate a Field with By.IDUses the id strategy on the same page and reads a different attribute.
- Type into a Text FieldTypes into a text field once you have found it.
- Choose One Radio in a GroupChooses one radio in a group, where several elements share a name.
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")
# Find name="uneditable" and print its value and tag
finally:
driver.quit()