0
0
Selenium Pythontesting~20 mins

Prompt alert with text input in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prompt Alert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Selenium Python code handling a prompt alert?
Consider the following Selenium Python code snippet that handles a prompt alert by sending text and accepting it. What will be printed?
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Assume driver is already initialized and navigated to page with prompt alert
alert_button = driver.find_element(By.ID, 'promptButton')
alert_button.click()

wait = WebDriverWait(driver, 10)
prompt = wait.until(EC.alert_is_present())
prompt.send_keys('Hello')
prompt.accept()

result_text = driver.find_element(By.ID, 'result').text
print(result_text)
AYou entered Hello
BNo alert present
CHello
DTimeoutException
Attempts:
2 left
💡 Hint
Think about what the page shows after accepting the prompt with input.
assertion
intermediate
1:30remaining
Which assertion correctly verifies the prompt alert input was accepted?
You want to verify that the prompt alert input 'Test123' was accepted and displayed on the page in element with ID 'result'. Which assertion is correct?
Selenium Python
result_text = driver.find_element(By.ID, 'result').text
Aassert result_text != ''
Bassert 'Test123' in result_text
Cassert result_text == 'You entered Test123'
Dassert result_text == 'Test123'
Attempts:
2 left
💡 Hint
Check the exact text the page shows after prompt input.
locator
advanced
1:30remaining
Which locator is best to find the prompt alert button for reliable tests?
You want to locate the button that triggers the prompt alert. The button has class 'btn' and text 'Click me'. Which locator is best practice for stable Selenium tests?
Adriver.find_element(By.XPATH, "//button[text()='Click me']")
Bdriver.find_element(By.TAG_NAME, 'button')
Cdriver.find_element(By.CSS_SELECTOR, 'button.btn')
Ddriver.find_element(By.CLASS_NAME, 'btn')
Attempts:
2 left
💡 Hint
Consider uniqueness and stability of locators.
🔧 Debug
advanced
2:00remaining
Why does this Selenium code raise UnexpectedAlertPresentException?
This code tries to send keys to a prompt alert but raises UnexpectedAlertPresentException. Why?
Selenium Python
alert_button = driver.find_element(By.ID, 'promptButton')
alert_button.click()
prompt = driver.switch_to.alert
prompt.send_keys('Test')
prompt.accept()

# Next line fails
driver.find_element(By.ID, 'result').text
AThe element with ID 'result' does not exist
Bsend_keys is not supported on alerts
Cprompt.accept() must be called before send_keys
DThe alert was not waited for and not ready when send_keys was called
Attempts:
2 left
💡 Hint
Check if the alert is fully present before interacting.
framework
expert
3:00remaining
How to design a reusable test function for prompt alerts with input and verification?
You want a reusable Python function using Selenium that clicks a prompt alert button, inputs text, accepts it, and verifies the result text. Which function design is best?
Selenium Python
def test_prompt_alert(driver, button_id, input_text, result_id, expected_text):
    # Fill in the function body
A
def test_prompt_alert(driver, button_id, input_text, result_id, expected_text):
    driver.find_element(By.ID, button_id).click()
    alert = driver.switch_to.alert
    alert.send_keys(input_text)
    alert.accept()
    result = driver.find_element(By.ID, result_id).text
    assert expected_text in result
B
def test_prompt_alert(driver, button_id, input_text, result_id, expected_text):
    driver.find_element(By.ID, button_id).click()
    alert = WebDriverWait(driver, 10).until(EC.alert_is_present())
    alert.send_keys(input_text)
    alert.accept()
    result = driver.find_element(By.ID, result_id).text
    assert result == expected_text
C
def test_prompt_alert(driver, button_id, input_text, result_id, expected_text):
    driver.find_element(By.ID, button_id).click()
    alert = WebDriverWait(driver, 5).until(EC.alert_is_present())
    alert.accept()
    alert.send_keys(input_text)
    result = driver.find_element(By.ID, result_id).text
    assert result == expected_text
D
def test_prompt_alert(driver, button_id, input_text, result_id, expected_text):
    driver.find_element(By.ID, button_id).click()
    alert = WebDriverWait(driver, 10).until(EC.alert_is_present())
    alert.send_keys(input_text)
    alert.dismiss()
    result = driver.find_element(By.ID, result_id).text
    assert result == expected_text
Attempts:
2 left
💡 Hint
Wait for alert, send keys, accept, then verify exact result.