About this lesson
Selenium Python send_keys: type into a text field and read the value back
Selenium Python AssignmentsBeginnerWeb Element Interactions
How it works
send_keys types into an element as if a user were at the keyboard. In Python you call it on the element, pass the text as a string, and it returns None. The typed text is added to whatever the field already holds, so a second call produces the combined text. An empty field ends up with only what you sent, while a pre-filled field ends up with the old text followed by the new.
This page has two elements with the same id. find_element returns the first one, which is the field you want, and the second is left alone. After typing, do not read .text: an input keeps its content in the value property, which get_attribute("value") reads, so .text stays empty. get_attribute("value") returns it as a str, and that is the proof the keys arrived.
Not every locked field reacts the same way. A disabled input raises ElementNotInteractableException: element not interactable, while a read-only one ignores the keys silently. A disabled input raises ElementNotInteractableException: element not interactable. An unknown attribute is not an error but a None, and joining None to a string with + raises TypeError. So type, read the value into a variable, then print it with its label.
About the assignment
Open the Text Fields page, type one word into the first element that carries the shared id, and print the value the field holds afterwards.
What you will do
- Find the field with the id strategy. Because the id is shared,
find_elementgives you the first match. - Call
send_keyson the element with the word from the task. - Read the field's
valueattribute, not its visible text. - Print the value after the label from the task.
Common mistakes
Reading .text after typing
The text of an
inputis always an empty string, so you print only the label. The typed characters live in the field'svalueproperty, whichget_attribute("value")reads. The HTML itself does not change.Typing twice and expecting a reset
Each call adds to the field. Sending the same word twice gives that word repeated, and typing into the pre-filled field keeps its old text first. Clearing is the next lesson's job.
Typing into a disabled field
The disabled input on this page rejects keys with
ElementNotInteractableException. Checkis_enabled()if you are not sure which field you hold.
In a real test suite
Test code seldom types literal strings in the middle of a test. Values come from a fixture or from parametrize, and a page object method such as fill_first_name(value) hides the locator, so one change fixes every test that fills the form.
Because send_keys appends, the next lesson shows how to empty a field and type again.
Related lessons
- Two Elements Share One IDExplains why the shared id on this page yields two matches and which one
find_elementreturns. - Clear a Field and Type AgainEmpties a field before typing again, which solves the appending behaviour.
- Read an AttributeReads attributes such as
valuefrom an element in more detail.
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")
# Type Selenium into the first #firstName and print the value
finally:
driver.quit()