Skip to content
CodeKruPro

Find Elements with CSS

13/80

Find Elements with CSS

A CSS selector can target an id or an id plus a tag. By.CSS_SELECTOR uses the same syntax as a stylesheet.

Task:

  1. Find #preFilledTextField with CSS
  2. Find input#disabledField with CSS
  3. Print both values

Print the lines below exactly.

Expected output:

Pre-filled value: Codekru
Disabled value: Codekru

About this lesson

Selenium Python By.CSS_SELECTOR with an id: #id and tag#id selectors

Selenium Python AssignmentsBeginnerCSS and XPath Locators

How it works

By.CSS_SELECTOR reuses the selector syntax from stylesheets. A hash sign followed by a name means "the element with this id", and a tag name placed in front narrows the match to that kind of element. So one selector can say "any element with this id", while another says "an input with this id".

The extra tag is a safety check. If the id moved to a different tag, the narrower selector would stop matching and find_element would raise NoSuchElementException. Here the tag form works because both fields are input elements. Naming a div matches nothing, even though the id exists.

Two Python details help. The selector is just a string, so quotes are only needed around the whole thing. And .text is empty for an input, so the contents come from get_attribute("value"). The second field is disabled, yet a disabled field still has a value you can read. Typing into it and clearing it are refused.

About the assignment

Find two fields on the Text Fields page with CSS only: one through its bare id and one through a tag plus its id. Both hold text you can read, and you report each value.

What you will do

  1. Build a CSS selector for the pre-filled field using the hash form, and pass it to find_element with the CSS strategy.
  2. Build a second selector that starts with the tag name, then adds the hash and the id of the disabled field.
  3. Read the value attribute from each element.
  4. Print the two lines in the order and with the labels from the task.

Common mistakes

  • Forgetting the hash sign

    A selector holding only the id name is read as a tag name. No tag has that name, so find_element raises NoSuchElementException.

  • Naming the wrong tag

    Putting div in front of the id matches nothing, because the id belongs to an input. Check the tag in the page source first.

  • Leaving a dangling hash

    A selector that ends with # and no name is not valid CSS. Selenium raises InvalidSelectorException: invalid selector, which is a different error from not finding the element.

In a real test suite

Most Python suites keep selectors in one place, often as tuples such as (By.CSS_SELECTOR, "...") in a page object, because the expected conditions in WebDriverWait take those tuples directly.

The next lesson stays with CSS but matches on other attributes, such as a placeholder or the start of a 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.

main.py
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
try:
    driver.get("https://testkru.com/Elements/TextFields")
    # Print both values using CSS selectors
finally:
    driver.quit()