Skip to content
CodeKruPro

Select All and Replace Text with the Keyboard

61/80

Select All and Replace Text with the Keyboard

Keys.CONTROL and "a" select the field. Backspace deletes the selection. This page has two #firstName fields. find_element uses the first.

Task:

  1. Type Hello World into the first #firstName
  2. Select all and delete it
  3. Type Selenium and print the value

Print the lines below exactly.

Expected output:

Final value: Selenium

About this lesson

Select all and replace text in Selenium Python with Keys.CONTROL and send_keys

Selenium Python AssignmentsAdvancedMouse and Keyboard

How it works

In Selenium Python, Keys.CONTROL is just a string constant that stands for the Control key. When you pass it to send_keys followed by a letter, Selenium presses the modifier and then types the letter, which selects the whole content of a field.

The modifier has to come first. With the letter first, the browser types the letter and then presses Control alone, so you only add a character. Once the text is selected, a Backspace or Delete key removes it, and plain typing on top replaces it.

An input keeps what the user typed in its value property, not in the visible text, so element.text gives you an empty string. Use get_attribute("value") instead. Also, send_keys adds to what is already there, so without the select step old and new text end up joined.

About the assignment

The Text Fields page has two inputs that share one id, and find_element returns the first. You type a phrase, wipe it with the keyboard instead of a clear call, type a different word, and report what the field holds.

What you will do

  1. Locate the first matching field and keep it in a variable.
  2. Type the starting phrase into it.
  3. Select everything with the Control key plus the letter, then remove the selection with a delete key. Both keys come from the Keys class that the starter already imports.
  4. Type the new word, read the field's value attribute, and print the label and that value as one line.

Common mistakes

  • Reading .text from the input

    Printing the element's text gives an empty string, with no error. Whatever you typed is held in the value attribute.

  • Putting the letter before the modifier

    Sending "a" and then Keys.CONTROL types an a and leaves the old text alone. After the next send_keys the field holds the old phrase with extra characters added at the end.

  • Concatenating None or a missing value

    A misspelled attribute name makes get_attribute return None. Joining that to a label with + raises a TypeError.

In a real test suite

Teams usually prefer clear() for resetting a field, since it needs no shortcut. The keyboard route earns its place when a page runs its own key handlers, such as a search box that reacts to each key press.

The next lesson holds a key down on purpose, so a whole word is typed with Shift pressed.

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
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
try:
    driver.get("https://testkru.com/Elements/TextFields")
    # Replace Hello World with Selenium in the first #firstName
finally:
    driver.quit()