Skip to content
CodeKruPro

Handle Confirm Alert

41/70

Handle Confirm Alert

A confirm alert has two buttons: OK and Cancel. Use accept() for OK and dismiss() for Cancel.

On the Alerts page, clicking "Show Confirm" shows a confirm dialog. The result appears in id="confirmResult".

Task:

  1. Navigate to https://testkru.com/Interactions/Alerts
  2. Click "Show Confirm" and accept the alert
  3. Print the confirm result text

Expected output:

Alert text: Do you want to proceed?
Result: You clicked: OK

About this lesson

Handle a confirm alert in Selenium Java: accept() for OK, dismiss() for Cancel

Learn Selenium JavaIntermediateAlerts & Popups

How it works

A confirm dialog, from JavaScript's confirm(), asks a yes or no question and returns the answer to the page. In Selenium both answers go through the same Alert: accept() presses OK and dismiss() presses Cancel.

Unlike a simple alert, the choice changes what the page does next. On the Alerts page the answer is written into the confirmResult paragraph, so the test can check that the page reacted to OK or to Cancel, not only that a dialog appeared.

A confirm has no text box. Calling sendKeys() on it throws ElementNotInteractableException with "User dialog does not have a text box input field".

About the assignment

Clicking the confirm button asks whether you want to proceed. You read the question, answer OK, and then read the page's result paragraph to see the answer it recorded.

What you will do

  1. Click the confirm button, which you locate by its label.
  2. Switch to the alert, keep its text, and accept it.
  3. Print the stored question after the label Alert text:.
  4. Read confirmResult from the page and print its text after the label Result:.

Common mistakes

  • Calling dismiss() by habit

    Cancel writes a different result into confirmResult, so the second line no longer matches.

  • Reading the result before answering

    The page is blocked while the dialog is open. Finding confirmResult first throws UnhandledAlertException.

  • Asserting only the dialog text

    The dialog proves the question was asked. The result paragraph proves the page acted on the answer, which is the part that matters to users.

In a real test suite

Delete buttons and "leave without saving" flows often use confirms. Good tests cover both branches: Cancel keeps the record, OK removes it.

Lesson 40 handled a simple alert. Lesson 42 adds a text box to the dialog with a prompt.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Interactions/Alerts");
        
        // TODO: Click "Show Confirm" button
        // TODO: Get alert text, then accept
        // TODO: Print alert text and the result from confirmResult element
        
        driver.quit();
    }
}