Bird
Raised Fist0
Selenium Pythontesting~10 mins

Prompt alert with text input in Selenium Python - Test Execution Trace

Choose your learning style10 modes available
Test Overview

This test opens a web page with a prompt alert, enters text into the prompt, accepts it, and verifies the result message on the page.

Test Code - Selenium
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
import unittest

class TestPromptAlert(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://the-internet.herokuapp.com/javascript_alerts')

    def test_prompt_alert_input(self):
        driver = self.driver
        # Click the button that triggers the prompt alert
        prompt_button = driver.find_element(By.XPATH, '//button[text()="Click for JS Prompt"]')
        prompt_button.click()

        # Wait for alert to be present
        WebDriverWait(driver, 10).until(EC.alert_is_present())

        # Switch to alert and send text
        alert = driver.switch_to.alert
        alert.send_keys('Hello Test')
        alert.accept()

        # Verify the result text
        result = driver.find_element(By.ID, 'result').text
        self.assertEqual(result, 'You entered: Hello Test')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and opens Chrome browserBrowser window opens at 'https://the-internet.herokuapp.com/javascript_alerts'—PASS
2Finds the button with text 'Click for JS Prompt' and clicks itPrompt alert dialog appears on screen—PASS
3Waits up to 10 seconds for alert to be presentAlert is present and active—PASS
4Switches to alert, sends text 'Hello Test', and accepts the alertAlert closes, page shows updated result text—PASS
5Finds element with id 'result' and verifies its text equals 'You entered: Hello Test'Page displays confirmation message with entered textAssert that result text is exactly 'You entered: Hello Test'PASS
6Test ends and browser closesBrowser window closes—PASS
Failure Scenario
Failing Condition: Alert does not appear or text in result does not match expected
Execution Trace Quiz - 3 Questions
Test your understanding
What action triggers the prompt alert in this test?
ANavigating to the page URL
BClicking the button labeled 'Click for JS Prompt'
CSending keys to the alert
DWaiting for the alert to appear
Key Result
Always wait explicitly for alerts before interacting with them to avoid timing issues, and verify the page updates after alert actions.

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

  1. Step 1: Understand prompt alerts

    Prompt alerts allow users to enter text in a popup box on the webpage.
  2. Step 2: Handling prompt alerts in Selenium

    Selenium can switch to the alert, send text input, and accept it to proceed.
  3. Final Answer:

    To enter text into a popup alert and accept it -> Option B
  4. 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

  1. Step 1: Switch to alert correctly

    Use driver.switch_to.alert to access the alert object.
  2. Step 2: Send keys and accept alert

    Call send_keys('hello') then accept() to input text and confirm.
  3. Final Answer:

    alert = driver.switch_to.alert alert.send_keys('hello') alert.accept() -> Option C
  4. Quick Check:

    switch_to.alert + send_keys + accept = correct [OK]
Hint: Always switch_to.alert before sending keys [OK]
Common Mistakes:
  • Using find_element instead of switch_to.alert
  • Calling dismiss() before send_keys
  • Sending keys without switching to alert
3. What will be the output of this Selenium Python code if the prompt alert expects a name and you input 'Alice'?
alert = driver.switch_to.alert
alert.send_keys('Alice')
alert.accept()
result = driver.find_element(By.ID, 'result').text
print(result)
medium
A. Alert dismissed without input
B. No alert found error
C. Empty string printed
D. Hello Alice! Your input was received.

Solution

  1. Step 1: Input text and accept alert

    The code sends 'Alice' to the prompt alert and accepts it, so input is registered.
  2. Step 2: Retrieve result text from page

    The page updates an element with ID 'result' to show a greeting including the input.
  3. Final Answer:

    Hello Alice! Your input was received. -> Option D
  4. Quick Check:

    Input 'Alice' + accept = greeting message [OK]
Hint: Accepted prompt input updates page text [OK]
Common Mistakes:
  • Expecting error without alert present
  • Not retrieving updated element text
  • Assuming alert dismiss clears input
4. Identify the error in this Selenium Python code snippet handling a prompt alert:
alert = driver.switch_to.alert
alert.accept()
alert.send_keys('Test')
medium
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

  1. Step 1: Understand alert lifecycle

    Calling accept() closes the alert, so it cannot receive input afterward.
  2. Step 2: Correct order for input and accept

    send_keys() must be called before accept() to input text successfully.
  3. Final Answer:

    send_keys called after accept, alert no longer available -> Option A
  4. 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?
hard
A. alert = driver.switch_to.alert alert.send_keys('') alert.accept() msg = driver.find_element(By.ID, 'error').text assert msg == 'Input required'
B. alert = driver.switch_to.alert alert.dismiss() msg = driver.find_element(By.ID, 'error').text assert msg == 'Input required'
C. alert = driver.switch_to.alert alert.send_keys('25') alert.dismiss() msg = driver.find_element(By.ID, 'error').text assert msg == 'Input required'
D. alert = driver.switch_to.alert alert.send_keys('') alert.dismiss() msg = driver.find_element(By.ID, 'error').text assert msg == 'Input required'

Solution

  1. Step 1: Send empty input and accept alert

    Send empty string to prompt and accept to simulate empty input submission.
  2. Step 2: Verify error message on page

    Check the element with ID 'error' for the rejection message 'Input required'.
  3. Final Answer:

    Send empty input + accept + assert error message -> Option A
  4. Quick Check:

    Empty input + accept triggers error message [OK]
Hint: Send empty string then accept to test input validation [OK]
Common Mistakes:
  • Dismissing alert instead of accepting
  • Not sending any input before accept
  • Checking wrong element for error message
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard