Skip to content
CodeKruPro

Locate a Field with By.ID

4/80

Locate a Field with By.ID

find_element(By.ID, ...) returns the first element with that id. An attribute is read with get_attribute.

Task:

  1. Open Text Fields
  2. Find #lastNameWithPlaceholder
  3. Print its placeholder

Print the lines below exactly.

Expected output:

Placeholder: Enter your last name...
Read full tutorial on Codekru

About this lesson

Selenium Python find_element with By.ID and get_attribute: read a placeholder

Selenium Python AssignmentsBeginnerLocators

How it works

A locator is a pair of values: a strategy and a search text. In Python you pass them as two arguments to driver.find_element(). By.ID is the strategy that looks at the id attribute, and ids are meant to be unique on a page, which makes this the most stable locator when a page offers one.

The call returns a WebElement. That object is a handle, not a copy of the text, and it has its own attributes and methods. get_attribute("name") returns the value of an HTML attribute as a str. For a misspelt or non-standard name it returns None, but a standard attribute that is absent, such as id or placeholder on an input, often returns an empty string. That detail matters in Python, because joining None to a label with + raises TypeError with the message can only concatenate str (not "NoneType") to str.

Matching is exact and case sensitive. A leading #, a different letter case or a typo gives NoSuchElementException, and the message echoes the selector Selenium tried, which helps when you debug.

About the assignment

You find one input on the Text Fields page by its id and read the hint text that the field shows when it is empty. The id is named in the starter comment.

What you will do

  1. Load the Text Fields page, which the starter already does.
  2. Find the element with that id using the id strategy, and keep the result in a variable.
  3. Ask the element for its placeholder attribute.
  4. Print the label Placeholder:, a space, and the value you got.

Common mistakes

  • Including the hash sign in the id

    The # is CSS syntax. With By.ID you pass the bare id, and a value that starts with # raises NoSuchElementException: no such element.

  • Reading .text from an input

    An input has no visible text, so .text returns an empty string and your line ends right after the label. Hint text lives in an attribute, so use get_attribute().

  • Misspelling the attribute name

    A misspelt attribute name returns None, and joining it to a label raises TypeError. Check the spelling against the page source.

In a real test suite

Real suites rarely scatter locators through tests. A page object keeps them as tuples, for example USERNAME = (By.ID, "user"), and a helper unpacks them with driver.find_element(*USERNAME). When an id changes, one line changes.

The next lesson finds a field through its name attribute and also reads the HTML tag of the element.

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 #lastNameWithPlaceholder and print its placeholder
finally:
    driver.quit()
Read the full tutorial