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
- Locate the first matching field and keep it in a variable.
- Type the starting phrase into it.
- Select everything with the Control key plus the letter, then remove the selection with a delete key. Both keys come from the
Keysclass that the starter already imports. - Type the new word, read the field's
valueattribute, and print the label and that value as one line.
Common mistakes
Reading .text from the input
Printing the element's
textgives an empty string, with no error. Whatever you typed is held in thevalueattribute.Putting the letter before the modifier
Sending
"a"and thenKeys.CONTROLtypes anaand leaves the old text alone. After the nextsend_keysthe field holds the old phrase with extra characters added at the end.Concatenating None or a missing value
A misspelled attribute name makes
get_attributereturnNone. Joining that to a label with+raises aTypeError.
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.
Related lessons
- Clear a Field and Type AgainClear a field with the built-in method and compare it with the keyboard approach.
- Type into a Text FieldPlain typing with
send_keys, which this lesson builds on. - Type Uppercase with the Shift KeyHold a modifier key down across several keystrokes.
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
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()