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
- Load the Text Fields page, which the starter already does.
- Find the element with that id using the id strategy, and keep the result in a variable.
- Ask the element for its
placeholderattribute. - Print the label
Placeholder:, a space, and the value you got.
Common mistakes
Including the hash sign in the id
The
#is CSS syntax. WithBy.IDyou pass the bare id, and a value that starts with#raisesNoSuchElementException: no such element.Reading .text from an input
An input has no visible text, so
.textreturns an empty string and your line ends right after the label. Hint text lives in an attribute, so useget_attribute().Misspelling the attribute name
A misspelt attribute name returns
None, and joining it to a label raisesTypeError. 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.
Related lessons
- Locate a Field with By.NAMEFinds a field by name and reads both its value and its tag.
- Fix a Locator That Does Not ExistShows what to do when a locator matches nothing at all.
- Read an AttributeGoes deeper on reading attributes from an 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.
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()