About this lesson
Selenium Python CSS attribute selector: exact match and starts-with (^=) in find_element
Selenium Python AssignmentsBeginnerCSS and XPath Locators
How it works
A CSS attribute selector goes in square brackets. An attribute name, an equals sign and a quoted value match elements whose attribute equals that text exactly. Replacing the equals sign with ^= changes the test to "starts with", so a prefix is enough. Both forms work with By.CSS_SELECTOR, and find_element returns the first match.
Exact means exact. The placeholder here ends with three dots, and a selector without them matches nothing and raises NoSuchElementException. Case counts too: a prefix with a capital letter does not match a lowercase one, though an i flag inside the brackets switches that off. Only one field has a placeholder, and only one name begins with the prefix, so both lookups are unambiguous.
Quotes are where Python learners trip. The value contains spaces, so it must be quoted inside the selector. Because the selector is a Python string, use one kind of quote outside and the other inside. Then read what you need: get_attribute("id") returns the id as a str, and the field value comes from the value attribute.
About the assignment
Find two fields without using their ids. One is located by the hint text in its placeholder, the other by the beginning of its name. Then print the id of the first and the contents of the second.
What you will do
- Write an attribute selector for the
placeholderwith the full text from the task, dots included. - Write a second selector that tests whether the
nameattribute starts with the prefix from the task. - From the first element read the
idattribute. From the second read thevalueattribute. - Print both lines with the labels from the task.
Common mistakes
Leaving the value unquoted
A value with spaces and no quotes is not valid CSS. Selenium raises
InvalidSelectorException: invalid selector. Use single quotes inside a double-quoted Python string.Shortening the placeholder
An exact match without the trailing dots fails with
NoSuchElementException. For a partial match use*=, which means "contains".Ignoring letter case in the prefix
A prefix that starts with a capital letter finds nothing, so
find_elementraisesNoSuchElementExceptionwhilefind_elementsreturns an empty list.
In a real test suite
Attribute selectors are popular in test suites because attributes such as data-testid survive visual redesigns. Teams add them on purpose, and a page object stores each selector once.
The next two lessons switch to XPath, which can express the same tests and also match visible text.
Related lessons
- Find Elements with CSSStarts with the simpler id and tag selectors that this lesson builds on.
- Find Elements with XPathThe XPath way to test an attribute, written with the at sign.
- Locate a Field with By.NAMEUses the
nameattribute on its own throughBy.NAME.
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")
# Match the placeholder and the name prefix with CSS
finally:
driver.quit()