Skip to content
CodeKruPro

Locate a Field with By.NAME

5/80

Locate a Field with By.NAME

By.NAME matches the name attribute. tag_name is the HTML tag.

Task:

  1. Find the element whose name is uneditable
  2. Print its value and its tag

Print the lines below exactly.

Expected output:

Value: Codekru
Tag: input

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

  1. Find the element using the name strategy and store it in a variable.
  2. Read the value attribute and print it after the label Value:.
  3. Read the tag of the same element from its property.
  4. 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() raises TypeError: 'str' object is not callable. It is a property that already holds a str.

  • Expecting text from an input

    Using .text on the field gives an empty string. What you see inside is the value attribute.

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.

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")
    # Find name="uneditable" and print its value and tag
finally:
    driver.quit()