Skip to content
CodeKruPro

Clear a Field and Type Again

21/80

Clear a Field and Type Again

clear() removes the current value. send_keys after that types into an empty field.

Task:

  1. Print the value of #preFilledTextField
  2. Clear it
  3. Type Selenium and print the value again

Print the lines below exactly.

Expected output:

Before: Codekru
After: Selenium

About this lesson

Replace pre-filled text with clear and send_keys in Selenium Python

Selenium Python AssignmentsBeginnerWeb Element Interactions

How it works

A text input can already hold a value when the page loads. If you call send_keys() on it, Selenium types at the end of that value. The old text stays and the new text is glued onto it.

clear() is the method that empties the field. It sends no replacement, so after it runs the field is an empty string. Typing is a second, separate action.

Reading the value needs care. The visible text of an input is always an empty string, because the typed content is not child text of the tag. The current content lives in the value attribute, and get_attribute() reads the live value. Its sibling get_dom_attribute() returns the attribute as it stands in the DOM, which does not change when you type.

About the assignment

Find the pre-filled text field, show what it holds, empty it, type a new word, and show what it holds now. You are proving that the old value was really replaced, not extended.

What you will do

  1. Locate the field by its id and keep it in a variable so you can reuse it.
  2. Read its value attribute and print a label followed by that value. The value comes back as a string, so joining it to a label works with +.
  3. Call clear(), then type the new word with send_keys().
  4. Read the value attribute a second time, from the same variable, and print it with the second label.

Common mistakes

  • Skipping clear() and appending by accident

    Calling only send_keys() leaves the old text in place, so the second read shows both old and new text joined together. The script runs without any error, which makes this easy to miss until you compare the output.

  • Reading .text on an input

    On an input, .text is an empty string. Use get_attribute() with the attribute name value instead.

  • Calling clear() on something that is not editable

    Running clear() on a heading raises InvalidElementStateException: invalid element state. Only inputs, textareas and editable elements can be cleared, so check that your locator found the field and not a label next to it.

In a real test suite

In a pytest suite, a form page object usually has one helper such as set_field(element, text) that clears first and then types. That way every test replaces values the same way, and edit-profile tests do not break when the form arrives pre-filled.

The next lesson stays on reading from the page, but switches from input values to the visible text of paragraphs.

  • Type into a Text FieldTypes into a text field first, which is the step that comes before replacing a value.
  • Read an AttributeGoes deeper on get_attribute(), the call used here to read the live value.
  • Read Visible TextShows how .text differs from an attribute, the trap behind the empty-input mistake.

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")
    # Clear #preFilledTextField and type Selenium
finally:
    driver.quit()