Prompt alert with text input in Selenium Python - Build an Automation Script
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.
Now add data-driven testing with 3 different inputs: 'Test1', '12345', and 'Hello!@#'
Practice
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 BQuick Check:
Prompt alert = text input + accept [OK]
- Confusing prompt alert with simple alerts
- Trying to send keys without switching to alert
- Ignoring the accept() step after input
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 CQuick Check:
switch_to.alert + send_keys + accept = correct [OK]
- Using find_element instead of switch_to.alert
- Calling dismiss() before send_keys
- Sending keys without switching to alert
alert = driver.switch_to.alert
alert.send_keys('Alice')
alert.accept()
result = driver.find_element(By.ID, 'result').text
print(result)Solution
Step 1: Input text and accept alert
The code sends 'Alice' to the prompt alert and accepts it, so input is registered.Step 2: Retrieve result text from page
The page updates an element with ID 'result' to show a greeting including the input.Final Answer:
Hello Alice! Your input was received. -> Option DQuick Check:
Input 'Alice' + accept = greeting message [OK]
- Expecting error without alert present
- Not retrieving updated element text
- Assuming alert dismiss clears input
alert = driver.switch_to.alert
alert.accept()
alert.send_keys('Test')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 AQuick Check:
accept() closes alert, no send_keys after [OK]
- Calling accept before send_keys
- Ignoring alert closure after accept
- Assuming alert object persists after accept
Solution
Step 1: Send empty input and accept alert
Send empty string to prompt and accept to simulate empty input submission.Step 2: Verify error message on page
Check the element with ID 'error' for the rejection message 'Input required'.Final Answer:
Send empty input + accept + assert error message -> Option AQuick Check:
Empty input + accept triggers error message [OK]
- Dismissing alert instead of accepting
- Not sending any input before accept
- Checking wrong element for error message
Ready to prove it?
Pass the test and this pattern moves unknown
