0
0
Selenium Pythontesting~15 mins

Prompt alert with text input in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Handle prompt alert by entering text and verifying result
Preconditions (1)
Step 1: Click the button that triggers the prompt alert
Step 2: Switch to the prompt alert
Step 3: Enter the text 'HelloTest' into the prompt input field
Step 4: Accept the prompt alert
Step 5: Verify that the page displays the text 'You entered: HelloTest'
✅ Expected Result: The page shows the message 'You entered: HelloTest' confirming the prompt input was accepted
Automation Requirements - selenium
Assertions Needed:
Verify the prompt alert is present before sending keys
Verify the displayed message matches the entered text
Best Practices:
Use explicit waits to wait for alert presence
Use By locators with meaningful identifiers
Handle alert switching properly
Use assertions from unittest or pytest
Automated Solution
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')
        self.wait = WebDriverWait(self.driver, 10)

    def test_prompt_alert_input(self):
        driver = self.driver
        wait = self.wait

        # Click the button that triggers the prompt alert
        prompt_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//button[text()="Click for JS Prompt"]')))
        prompt_button.click()

        # Wait for alert to be present
        alert = wait.until(EC.alert_is_present())

        # Enter text into prompt
        alert.send_keys('HelloTest')

        # Accept the alert
        alert.accept()

        # Verify the result text
        result = wait.until(EC.visibility_of_element_located((By.ID, 'result')))
        self.assertEqual(result.text, 'You entered: HelloTest')

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

if __name__ == '__main__':
    unittest.main()

This test uses Selenium WebDriver with Python's unittest framework.

In setUp, we open the browser and navigate to the test page.

We locate the button by its visible text using XPath and click it to trigger the prompt alert.

We use an explicit wait to wait until the alert is present before interacting with it.

We send the text 'HelloTest' to the prompt input and accept the alert.

Finally, we wait for the result message to appear and assert it matches the expected text.

In tearDown, we close the browser to clean up.

This approach ensures the test waits properly for elements and alerts, uses clear locators, and verifies the expected outcome.

Common Mistakes - 4 Pitfalls
Not waiting for the alert before sending keys
Using hardcoded XPath without meaningful context
Not switching to alert before sending keys or accepting
Not verifying the result message after alert interaction
Bonus Challenge

Now add data-driven testing with 3 different inputs: 'Test1', '12345', and 'Hello!@#'

Show Hint