Skip to content
CodeKruPro

Locate a Field with By.CLASS_NAME

6/80

Locate a Field with By.CLASS_NAME

By.CLASS_NAME takes one class token, not a string of several classes.

Task:

  1. Find the element with class last-name-input
  2. Print its id

Print the lines below exactly.

Expected output:

Element ID: lastNameWithPlaceholder

About this lesson

Selenium Python By.CLASS_NAME: find an element by one class and read its id

Selenium Python AssignmentsBeginnerLocators

How it works

An HTML element can carry several classes in one class attribute, separated by spaces, such as btn btn-primary. By.CLASS_NAME matches elements that include a given class, and it accepts only one class name at a time. That limit is the main thing to remember about this locator.

If you pass two class names in one string, Selenium does not search for either. It raises InvalidSelectorException with the message Compound class names are not allowed. When you need two classes at once, switch to a CSS selector, which a later lesson covers.

Class names are shared by design, so many elements can match. find_element hands back the first match in page order, and find_elements returns a list. Reading the id of the match with get_attribute("id") is a handy way to confirm that you landed on the element you meant, and it returns a str.

About the assignment

You find the field that carries a particular class from the Text Fields page, then report its id. The class is named in the starter comment, and it is a single token, so the locator is simple.

What you will do

  1. Use the class strategy with the single class name from the starter comment.
  2. Keep the returned element in a variable.
  3. Read the id attribute from it, which gives back a string.
  4. Print the label Element ID:, a space, and that id.

Common mistakes

  • Passing two classes at once

    Giving a string with a space raises InvalidSelectorException: Compound class names are not allowed. Use one class, or move to a CSS selector.

  • Starting the class with a dot

    The dot belongs to CSS. With By.CLASS_NAME it gives InvalidSelectorException, so write the bare class name.

  • Using a class that is not on the page

    A class name with a typo raises NoSuchElementException. Compare it against the page source, because classes often look alike.

In a real test suite

Classes change often when designers restyle a page, so many teams treat them as a weaker locator than ids or test attributes. They are still handy for groups, for instance finding every row of a table and counting the results.

Next you locate a heading by its tag, which is another locator that usually matches many elements.

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 class last-name-input and print its id
finally:
    driver.quit()