Introduction
A Prompt Alert is a JavaScript popup that requests input from the user before continuing. Unlike standard alerts or confirmation alerts, a prompt alert contains a text input field along with OK and Cancel buttons.
Prompt alerts are commonly used when an application needs simple user input, such as a name, email address, or confirmation text. Selenium handles prompt alerts using the Alert interface.
In this tutorial, you’ll learn how to automate prompt alerts using Selenium with Python, along with practical examples, real-world scenarios, common mistakes, and best practices.
What is a Prompt Alert?
A Prompt Alert is a browser popup that allows users to enter text before confirming or cancelling the action.
It contains:
A message
Text input field
OK button
Cancel button
Example:
---------------------------------
Please enter your name
[_______________]
[ OK ] [ Cancel ]
---------------------------------
The application processes the entered text after the user clicks OK.
Why Automate Prompt Alerts?
Automating prompt alerts helps you:
Verify text input.
Test user interaction.
Validate application responses.
Improve automation coverage.
Test browser-based input dialogs.
Example
from selenium import webdriver
from selenium.webdriver.common.by import By
# Topic: 29. Alerts and Frames - Prompt Alerts
# Practice site: https://the-internet.herokuapp.com/javascript_alerts
# Run: pytest -s 29_examples/test_01_prompt_alerts.py
#
# A prompt alert accepts text input. Selenium switches from the page to the
# alert, sends text, accepts it, and then returns control to the page.
def test_prompt_alert():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/javascript_alerts")
driver.find_element(By.XPATH, "//button[text()='Click for JS Prompt']").click()
alert = driver.switch_to.alert
alert.send_keys("Selenium Python")
alert.accept()
result = driver.find_element(By.ID, "result").text
assert "Selenium Python" in result
finally:
driver.quit()
Understanding the Code
Import Required Libraries
from selenium import webdriver
from selenium.webdriver.common.by import By
These modules are required to launch the browser and locate web elements.
Create a Chrome Browser Instance
driver = webdriver.Chrome()
Launches a new Chrome browser session.
Open the Practice Website
driver.get("https://the-internet.herokuapp.com/javascript_alerts")
Navigates to the JavaScript Alerts practice page.
Click the “JS Prompt” Button
driver.find_element(
By.XPATH,
"//button[text()='Click for JS Prompt']"
).click()
Locates the Click for JS Prompt button using XPath and clicks it to display the prompt alert.
Switch Control to the Prompt Alert
alert = driver.switch_to.alert
Since JavaScript alerts are browser popups and not part of the webpage, Selenium switches control from the webpage to the alert.
Enter Text into the Prompt
alert.send_keys("Selenium Python")
Enters “Selenium Python” into the prompt’s text field.
Accept the Prompt
alert.accept()
Clicks the OK button, submitting the entered text to the application.
Verify the Result
result = driver.find_element(By.ID, "result").text
assert "Selenium Python" in result
After the prompt closes, the webpage displays a confirmation message.
The assertion verifies that the entered text “Selenium Python” appears in the result, confirming that the prompt was handled successfully.
Close the Browser
driver.quit()
Closes all browser windows and ends the WebDriver session.
Practical Example
Suppose a customer feedback portal asks the user to enter their name before submitting feedback.
The automation script:
Opens the prompt.
Enters John Smith.
Clicks OK.
Verifies that the application displays the entered name.
Automation Testing Example
Consider an online survey application.
Before starting the survey, the application asks the user to enter a participant ID.
The automation script:
Opens the prompt alert.
Enters EMP1001.
Accepts the prompt.
Verifies that the survey starts successfully.
Real-World Example
Prompt alerts are commonly used in:
Survey applications
Feedback systems
Administrative portals
Internal enterprise applications
Educational platforms
Demo applications for JavaScript
Although prompt alerts are less common than confirmation alerts, they are still used in some legacy and browser-based applications.
Advantages of Automating Prompt Alerts
Verifies text input functionality.
Tests browser dialogs.
Validates user input handling.
Improves automation coverage.
Simulates real user interaction.
Common Mistakes Beginners Make
Forgetting to Switch to the Prompt
Always switch first:
driver.switch_to.alert
Trying to Use send_keys() on the Webpage
The input field belongs to the browser prompt, not the webpage.
Always call:
alert.send_keys("John")
Ignoring the Prompt
If the prompt is left open, Selenium cannot continue interacting with the webpage and may throw an UnhandledAlertException.
Not Verifying the Prompt Message
Always verify that the application correctly processes the entered text after accepting the prompt.
Best Practices
Switch to the prompt before interacting.
Verify the prompt message whenever required.
Enter meaningful test data using
send_keys().Validate the result displayed on the webpage after accepting the prompt.
Test both OK and Cancel scenarios.
Handle the prompt before interacting with webpage elements again.
Conclusion
Prompt alerts are JavaScript dialogs that request user input before continuing. Selenium automates prompt alerts by switching to the alert, entering text using send_keys(), and accepting or dismissing the dialog. Verifying the application’s response after handling the prompt ensures that user input is processed correctly and improves the reliability of automation tests.
Frequently Asked Questions (FAQs)
What is a prompt alert?
A prompt alert is a JavaScript popup that asks the user to enter text before continuing.
How do I enter text into a prompt alert?
Use:
alert.send_keys("Your Text")
How do I submit the entered text?
Use:
alert.accept()
Can Selenium automate prompt alerts?
Yes.
Selenium can read the prompt message, enter text, and either accept or dismiss the prompt.
Are prompt alerts commonly used in web applications?
They are less common than standard or confirmation alerts but are still found in survey systems, feedback portals, administrative applications, and some legacy web applications.
Key Takeaways
Prompt alerts allow users to enter text before continuing.
Use
driver.switch_to.alertto access the prompt.Use
send_keys()to provide input.Use
accept()to submit the entered value.Verify that the webpage correctly processes the submitted text.
Prompt alert automation is useful for testing browser-based input dialogs.
