Skip to content
CodeKruPro

Fix a Locator That Does Not Exist

12/80

Fix a Locator That Does Not Exist

A locator that matches nothing raises NoSuchElementException and the script stops. finally still quits the browser. Change the id to one that is on the page.

Task:

  1. Run the starter and read the traceback
  2. Replace missingField with preFilledTextField
  3. Print the field value

The starter looks up an id that is not on the page. The fix is the real id, not a second try of the same missing id.

Print the lines below exactly.

Expected output:

Value: Codekru

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

  1. Run the starter unchanged and find the line starting selenium.common.exceptions, above Stacktrace:. It names the exception and echoes the id that was searched for.
  2. 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.
  3. Keep the line that reads the value attribute.
  4. Print the label from the task followed by that value.

Common mistakes

  • Silencing the exception

    Wrapping the lookup in except NoSuchElementException: pass leaves 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 .text on the field returns an empty string and raises nothing. Joining a missing attribute breaks differently: get_attribute returns None, and + raises TypeError with the message can 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.

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")
    field = driver.find_element(By.ID, "missingField")
    print("Value: " + field.get_attribute("value"))
finally:
    driver.quit()