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
- Find the button by its exact label with an XPath text match and click it.
- Switch to the alert and store its message in a
String. - Call
accept()to close it. - Print the stored message after the label
Alert text:.
Common mistakes
Reading the text after accept()
The alert is gone, so
getText()throwsNoAlertPresentException. Read first, then accept.Touching the page before closing the alert
Any page command throws
UnhandledAlertExceptionand 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()throwsNoAlertPresentException; 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.
Related lessons
- Handle Confirm AlertConfirm dialogs, where OK and Cancel lead to different results.
- XPath - BasicsLocate a button by its text with XPath, since these buttons have no ids.
- Switch to Frame by IDAnother
switchTo()target: frames inside the page.
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.
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();
}
}