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
- Use the class strategy with the single class name from the starter comment.
- Keep the returned element in a variable.
- Read the
idattribute from it, which gives back a string. - 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_NAMEit givesInvalidSelectorException, 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.
Related lessons
- Find Elements with CSSIntroduces CSS selectors, the syntax to reach for when this locator is too limited.
- Locate a Field with By.IDFinds an element by id, the strategy that is meant to be unique.
- Fix a Locator That Does Not ExistRepairs a broken locator that matches nothing on the page.
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 class last-name-input and print its id
finally:
driver.quit()