About this lesson
Selenium Python prompt alert: type text with send_keys and read what the page shows
Selenium Python AssignmentsIntermediateAlerts and Popups
How it works
A prompt is an alert with a text box. The Alert object from driver.switch_to.alert has a send_keys method that types into that box, followed by accept() to press OK. Only a prompt has a box. Trying send_keys on a plain alert raises ElementNotInteractableException with the message "User dialog does not have a text box input field."
This prompt starts with a default value already filled in. Typing does not add to it. Calling send_keys replaces the default, and a second call replaces the first one again, so the last text you send is what stays. If you press OK without typing anything, the default text is submitted. If you press Cancel with dismiss(), the page shows a cancelled message instead.
Read the answer from the page, not from the alert. After you accept, the dialog is gone, and the page has written the value into a result element. Use find_element and its text attribute for that. Keep all the page lookups until the dialog has closed, because looking up elements while it is open raises UnexpectedAlertPresentException.
About the assignment
Open the prompt, replace its default text with your own word, submit it, and print what the page records.
What you will do
- Click the Show Prompt button, found by its visible text.
- Switch to the alert and keep it in a variable.
- Type the word Selenium into the prompt with
send_keys, then accept the dialog. - Read the prompt result element on the page and print its text after the label.
Common mistakes
Using send_keys on the wrong dialog
A simple alert or a confirm has no text box. The call fails with
ElementNotInteractableException: element not interactable: User dialog does not have a text box input field.Forgetting to accept
Typing alone leaves the dialog open. The next page lookup then raises
UnexpectedAlertPresentException, and the typed text is lost when Chrome closes the dialog.Printing the alert instead of the page
Once accepted, the
Alertobject has nothing to read. The value you typed now lives in the result element on the page.
In a real test suite
Prompts are rare in modern apps, but legacy admin screens still use them. A suite handles them in one helper that takes the text and a choice of accept or dismiss, and the test then asserts on the page element, not on the dialog.
The next lesson handles an awkward case: a second alert that only opens once the first one is closed.
Related lessons
- Accept and Dismiss a ConfirmExplains
accept()anddismiss()on a dialog with two buttons. - Accept a Simple AlertThe simplest dialog, with no text box to fill.
- Handle Two Alerts in a RowTwo dialogs one after another.
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/Interactions/Alerts")
# Type Selenium into the prompt and print #promptResult
finally:
driver.quit()