Introduction
Handling JavaScript alerts is one of the most common Selenium interview topics and an important automation skill. Applications frequently display alerts while deleting records, confirming transactions, submitting forms, or showing validation messages.
Selenium provides the Alert interface to interact with these browser popups.
Using Selenium, you can:
- Accept alerts
- Dismiss alerts
- Enter text into prompt alerts
- Read alert messages
- Handle delayed alerts
- Wait until alerts appear
- Handle unexpected alerts gracefully
This tutorial contains practical exercises that help you master all these scenarios.
Types of Selenium Alerts
There are three primary JavaScript alert types.
Simple Alert
A simple alert displays a message and provides only one option.
Available Action
- Accept (OK)
Selenium Method
alert.accept();
Confirmation Alert
A confirmation alert asks users to either continue or cancel an action.
Available Actions
- Accept
- Dismiss
Selenium Methods
alert.accept();
alert.dismiss();
Prompt Alert
A prompt alert accepts user input before submission.
Available Actions
- Enter text
- Accept
- Dismiss
Selenium Methods
alert.sendKeys("TestUser");
alert.accept();
Hands-On Practice Exercises
Exercise 1 – Handle a Simple Alert
Objective
Navigate to a webpage containing a simple alert.
Task
- Trigger the alert
- Accept it
- Verify the alert disappears
- Verify the application continues normally
Focus
Basic alert handling using accept().
Exercise 2 – Handle a Confirmation Alert (Accept)
Objective
Work with confirmation dialogs.
Task
- Trigger the confirmation alert
- Click OK
- Verify the expected action completed successfully
Example
Deleting a record and confirming deletion.
Exercise 3 – Handle a Confirmation Alert (Dismiss)
Objective
Cancel an action.
Task
- Trigger confirmation alert
- Click Cancel
- Verify no changes were made
Focus
Testing cancellation scenarios.
Exercise 4 – Handle a Prompt Alert (Enter Text)
Objective
Enter text inside a prompt alert.
Task
- Open prompt alert
- Enter text
Test
- Accept the alert
- Verify the submitted text appears correctly
Exercise 5 – Handle a Prompt Alert (Dismiss)
Objective
Cancel prompt submission.
Task
- Open prompt alert
- Enter text (optional)
- Dismiss the alert
- Verify no data is submitted
Exercise 6 – Combined Alert Flow
Perform multiple alert operations within one test.
Workflow
- Handle simple alert
- Accept it
- Handle confirmation alert
- Dismiss it
- Handle prompt alert
- Enter text
- Accept it
- Verify all responses
Focus
Real-world workflow automation.
Exercise 7 – Handle NoAlertPresentException
Objective
Learn exception handling.
Task
- Trigger an alert
- Close it manually
- Selenium attempts to accept it again
- Handle the exception gracefully
Focus
Unexpected alert situations.
Exercise 8 – Wait for Delayed Alert
Objective
Handle AJAX or delayed alerts.
Task
- Trigger delayed alert
- Wait until alert appears
- Accept it
Focus
Using explicit waits.
Exercise 9 – Handle Multiple Alerts
Objective
Process multiple alerts sequentially.
Task
- Accept first alert
- Dismiss second alert
- Verify both actions
Exercise 10 – Handle Alert Inside an iFrame
Objective
Switch into an iframe before handling alerts.
Task
- Switch to iframe
- Trigger alert
- Accept alert
- Switch back to default content
- Verify application flow
Real-Time Practical Interview Questions
1. Where do you handle alerts in your Selenium framework?
2. What is the difference between simple, confirmation, and prompt alerts?
3. What problems have you faced when alerts appear after a delay?
4. How do you handle alerts if a test fails midway?
5. Have you handled multiple alerts within a single workflow?
6. How do you automate alerts inside iframes or modal windows?
7. Have you dismissed alerts in production instead of accepting them? Why?
8. How do you validate alert text before entering input into a prompt alert?
9. How do you prevent NoAlertPresentException?
10. How do you handle alerts while running tests in Jenkins or CI/CD pipelines?
Scenario-Based Questions
Scenario 1 – Delete Confirmation Alert
A user deletes a record.
Questions
- How do you accept the alert?
- How do you verify deletion?
- How do you dismiss the alert?
- How do you verify the record still exists?
Scenario 2 – Prompt Alert
A username must be entered into a prompt alert.
Questions
- How do you enter text?
- How do you verify the submitted value?
- How do you validate cancellation?
Scenario 3 – Unexpected Alert
An unexpected alert randomly appears during execution.
Questions
- How do you detect it?
- How do you prevent test failures?
Scenario 4 – Alerts During Parallel Execution
Multiple tests trigger alerts simultaneously.
Questions
- How do you ensure the correct alert is handled?
- How do you prevent cross-thread failures?
Scenario 5 – AJAX Alert
An alert appears five seconds after clicking a button.
Question
How do you wait until the alert appears without using Thread.sleep()?
Site-Based Practice Exercises
Confirmation Alert
driver.findElement(By.id("confirmButton")).click();
Alert confirmAlert = driver.switchTo().alert();
confirmAlert.accept();
// or
confirmAlert.dismiss();
Practice Scenario
Delete a record and verify behavior after accepting or dismissing the alert.
Prompt Alert (DemoQA Alerts)
driver.findElement(By.id("promtButton")).click();
Alert promptAlert = driver.switchTo().alert();
promptAlert.sendKeys("TestUser");
promptAlert.accept();
Practice Scenario
Enter a username and verify it is displayed correctly after submission.
Delayed Alert (DemoQA)
driver.findElement(By.id("timerAlertButton")).click();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
Practice Scenario
Handle alerts generated after AJAX requests or delayed server responses.
Multiple Alerts
Practice the following workflow.
- Simple alert → Accept
- Confirmation alert → Dismiss
- Prompt alert → Enter text
- Accept prompt
- Verify all application responses
Unexpected Alert Handling
try {
Alert alert = driver.switchTo().alert();
alert.accept();
}
catch (NoAlertPresentException e) {
// Continue execution
}
Practice Scenario
Safely handle unexpected alerts without causing test failures.
Frequently Asked Questions
What are the main methods available in Selenium's Alert interface?
The most commonly used methods are:
accept()dismiss()sendKeys()getText()
How do you handle delayed alerts?
Use an explicit wait with ExpectedConditions.alertIsPresent() before interacting with the alert.
How can you avoid NoAlertPresentException?
You can either:
- Wait until the alert is present using explicit waits.
- Handle the exception using a try-catch block.
How do you handle alerts inside an iframe?
Switch to the iframe first, trigger the alert, handle it, then switch back to the default content.
Which website is best for Selenium alert practice?
DemoQA Alerts is one of the best websites because it includes:
- Simple alerts
- Timed alerts
- Confirmation alerts
- Prompt alerts
All alert types are available on a single practice page.