Skip to content
CodeKruPro

Type into a Prompt

46/80

Type into a Prompt

A prompt accepts typed text. send_keys on the alert, then accept(). In this browser, typing Selenium replaces the default text Testkru.

Task:

  1. Click Show Prompt
  2. Type Selenium into the alert and accept it
  3. Print #promptResult

Print the lines below exactly.

Expected output:

Result: You entered: Selenium

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

  1. Click the Show Prompt button, found by its visible text.
  2. Switch to the alert and keep it in a variable.
  3. Type the word Selenium into the prompt with send_keys, then accept the dialog.
  4. 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 Alert object 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.

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/Interactions/Alerts")
    # Type Selenium into the prompt and print #promptResult
finally:
    driver.quit()