Skip to content
CodeKruPro

Match Attributes with CSS

14/80

Match Attributes with CSS

CSS can match an attribute exactly, or match the start of a value with ^=.

Task:

  1. Find the element whose placeholder is Enter your last name...
  2. Find the element whose name starts with pre
  3. Print the id of the first and the value of the second

Print the lines below exactly.

Expected output:

Found by placeholder: lastNameWithPlaceholder
Found by name prefix: Codekru
Read full tutorial on Codekru

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

  1. Write an attribute selector for the placeholder with the full text from the task, dots included.
  2. Write a second selector that tests whether the name attribute starts with the prefix from the task.
  3. From the first element read the id attribute. From the second read the value attribute.
  4. 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_element raises NoSuchElementException while find_elements returns 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.

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")
    # Match the placeholder and the name prefix with CSS
finally:
    driver.quit()
Read the full tutorial