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
- Build a CSS selector for the pre-filled field using the hash form, and pass it to
find_elementwith the CSS strategy. - Build a second selector that starts with the tag name, then adds the hash and the id of the disabled field.
- Read the
valueattribute from each element. - 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_elementraisesNoSuchElementException.Naming the wrong tag
Putting
divin front of the id matches nothing, because the id belongs to aninput. 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 raisesInvalidSelectorException: 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.
Related lessons
- Locate a Field with By.IDThe same kind of lookup through
By.ID, which this lesson rewrites as CSS. - Match Attributes with CSSExtends CSS selectors to attribute tests such as an exact match or a prefix.
- Check Whether a Text Field Is EnabledChecks whether a text field is enabled, which is how code tells the disabled field apart.
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")
# Print both values using CSS selectors
finally:
driver.quit()