About this lesson
Selenium Python NoSuchElementException: read the traceback and fix a locator that matches nothing
Selenium Python AssignmentsBeginnerLocators
How it works
NoSuchElementException is the most common error in a first Selenium Python script. find_element raises it when the locator matches nothing on the current page. Python prints a traceback that runs from your script into the Selenium library, and the line after the last File entry holds the exception name and a message. Ignore the hex Stacktrace below it. That message shows the selector that was tried, so you can compare it with the real HTML.
In Selenium 4.47 an id lookup is sent to the browser as a CSS attribute selector, and the message shows exactly that. Ids are case sensitive, so a wrong capital letter fails like a wrong name. Other causes are an element in another frame, or one that is not on the page yet.
The starter uses try and finally. When the exception is raised, Python still runs the finally block, so driver.quit() closes Chrome before the traceback is shown. Also remember that an input has empty .text. Its content sits in the value attribute, and get_attribute returns a string, an empty one for some absent standard attributes, or None for names the element does not know.
About the assignment
The starter fails on purpose. You run it, read the error, and repair the locator so it points at an id that really exists on the Text Fields page, then report the value that field holds.
What you will do
- Run the starter unchanged and find the line starting
selenium.common.exceptions, aboveStacktrace:. It names the exception and echoes the id that was searched for. - Change only the id text to the pre-filled field's id given in the task. Do not add a retry with the old id.
- Keep the line that reads the
valueattribute. - Print the label from the task followed by that value.
Common mistakes
Silencing the exception
Wrapping the lookup in
except NoSuchElementException: passleaves the real problem in place. The script then fails later, with a message that points away from the cause.Getting the id case wrong
The same name written in lowercase also raises
NoSuchElementException, because ids are case sensitive. Copy the id from the page source.Reading text from an input
Calling
.texton the field returns an empty string and raises nothing. Joining a missing attribute breaks differently:get_attributereturnsNone, and+raisesTypeErrorwith the messagecan only concatenate str (not "NoneType") to str.
In a real test suite
In a pytest suite a missing element fails the test with this same traceback. Good teams catch it inside a helper such as wait_for(locator) and re-raise a clearer error that names the page and the locator, so a red build explains itself.
The next lesson keeps the fix-the-locator habit but moves from ids to CSS selectors.
Related lessons
- Locate a Field with By.IDThe plain id lookup that this lesson breaks and then repairs.
- Two Elements Share One IDThe opposite case:
find_elementsreturns a list and never raises when nothing matches. - Read an AttributeGoes deeper into 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")
field = driver.find_element(By.ID, "missingField")
print("Value: " + field.get_attribute("value"))
finally:
driver.quit()