Skip to content
CodeKruPro

Handle Simple Alert

40/70

Handle Simple Alert

JavaScript alerts are handled using driver.switchTo().alert(). A simple alert has text and an OK button.

Methods:

  • alert.getText() - get alert message
  • alert.accept() - click OK
  • alert.dismiss() - click Cancel (for confirm/prompt)

Task:

  1. Navigate to https://testkru.com/Interactions/Alerts
  2. Click the "Show Alert" button
  3. Switch to the alert, get its text
  4. Accept (dismiss) the alert
  5. Print the alert text

Expected output:

Alert text: This is a simple alert!

About this lesson

Handle a JavaScript alert in Selenium Java: switchTo().alert(), getText() and accept()

Learn Selenium JavaIntermediateAlerts & Popups

How it works

A JavaScript alert() is a browser dialog, not part of the page, so findElement() cannot reach it. You switch to it with driver.switchTo().alert(), which returns an Alert. getText() reads its message and accept() presses OK.

While an alert is open, the page is blocked. Any other command, even getTitle(), throws UnhandledAlertException with the alert's text in the message, and Selenium's default behaviour then dismisses the alert, so a following switchTo().alert() finds nothing.

Switching when no alert is open throws NoAlertPresentException: no such alert. The same happens if you call getText() on an Alert you already accepted, which is why the text must be read before accept().

About the assignment

The Alerts page has a button that opens a simple alert. The buttons have no ids, so you locate this one by its text. You open the alert, read its message, close it, and print the message.

What you will do

  1. Find the button by its exact label with an XPath text match and click it.
  2. Switch to the alert and store its message in a String.
  3. Call accept() to close it.
  4. Print the stored message after the label Alert text:.

Common mistakes

  • Reading the text after accept()

    The alert is gone, so getText() throws NoAlertPresentException. Read first, then accept.

  • Touching the page before closing the alert

    Any page command throws UnhandledAlertException and the alert is dismissed for you, which can hide the step that went wrong.

  • Trying to handle the modal like an alert

    The page's "Open Modal" button shows an HTML modal, not a browser dialog. switchTo().alert() throws NoAlertPresentException; locate the modal's buttons with normal locators instead.

In a real test suite

Native alerts are less common in modern apps, but still appear for "Are you sure?" checks, session timeout warnings and older admin pages. When an alert may appear late, wait for it with ExpectedConditions.alertIsPresent() instead of switching straight away.

Lesson 39 finished the waits. The next three lessons handle the other dialog types on this page: confirm, prompt, and alerts that come one after another.

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 Alert" button
        // TODO: Switch to alert and get text
        // TODO: Accept the alert
        // TODO: Print alert text
        
        driver.quit();
    }
}