This script opens a page with a button that triggers a prompt alert. It types 'Alice' into the prompt and accepts it. Then it prints the page text which should greet Alice.
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# Setup driver
options = webdriver.ChromeOptions()
options.add_argument('--headless=new')
driver = webdriver.Chrome(options=options)
try:
# Open a simple page with prompt alert
driver.get('data:text/html,<script>function showPrompt() { var result = prompt(\'Enter your name:\'); document.body.innerHTML = result ? `Hello, ${result}!` : 'No name entered.'; }</script><button onclick="showPrompt()">Click me</button>')
# Click the button to open prompt
button = driver.find_element(By.TAG_NAME, 'button')
button.click()
# Switch to prompt alert
alert = driver.switch_to.alert
# Send text input
alert.send_keys('Alice')
# Accept the prompt
alert.accept()
time.sleep(1) # Wait for page update# Verify the page shows the input
body_text = driver.find_element(By.TAG_NAME, 'body').text
print(body_text)
finally:
driver.quit()
OutputSuccess
Important Notes
Always switch to the alert before interacting with it.
Use accept() to click OK and dismiss() to click Cancel.
Prompt alerts pause script execution until handled.
Summary
Prompt alerts ask users for text input in a popup.
Use switch_to.alert to access and send_keys() to type text.
Accept or dismiss the alert to continue the test.
Practice
(1/5)
1. What is the main purpose of handling a prompt alert with text input in Selenium?
easy
A. To scroll the webpage automatically
B. To enter text into a popup alert and accept it
C. To click a button on the webpage
D. To take a screenshot of the alert
Solution
Step 1: Understand prompt alerts
Prompt alerts allow users to enter text in a popup box on the webpage.
Step 2: Handling prompt alerts in Selenium
Selenium can switch to the alert, send text input, and accept it to proceed.
Final Answer:
To enter text into a popup alert and accept it -> Option B
Quick Check:
Prompt alert = text input + accept [OK]
Hint: Prompt alert means input text then accept [OK]
Common Mistakes:
Confusing prompt alert with simple alerts
Trying to send keys without switching to alert
Ignoring the accept() step after input
2. Which Selenium Python code snippet correctly sends text 'hello' to a prompt alert and accepts it?
easy
A. driver.send_keys('hello')
alert.accept()
B. alert = driver.find_element_by_id('alert')
alert.send_keys('hello')
alert.accept()
C. alert = driver.switch_to.alert
alert.send_keys('hello')
alert.accept()
D. alert = driver.switch_to.alert
alert.dismiss()
alert.send_keys('hello')
Solution
Step 1: Switch to alert correctly
Use driver.switch_to.alert to access the alert object.
Step 2: Send keys and accept alert
Call send_keys('hello') then accept() to input text and confirm.
Final Answer:
alert = driver.switch_to.alert
alert.send_keys('hello')
alert.accept() -> Option C
A. send_keys called after accept, alert no longer available
B. Missing import for Alert class
C. Incorrect locator used for alert
D. No error, code works fine
Solution
Step 1: Understand alert lifecycle
Calling accept() closes the alert, so it cannot receive input afterward.
Step 2: Correct order for input and accept
send_keys() must be called before accept() to input text successfully.
Final Answer:
send_keys called after accept, alert no longer available -> Option A
Quick Check:
accept() closes alert, no send_keys after [OK]
Hint: Send keys before accept to avoid alert closed error [OK]
Common Mistakes:
Calling accept before send_keys
Ignoring alert closure after accept
Assuming alert object persists after accept
5. You want to test a prompt alert that asks for user age and rejects empty input. Which Selenium Python code correctly handles this scenario and verifies the rejection message?