0
0
Selenium-pythonHow-ToBeginner · 4 min read

How to Get Alert Text in Selenium: Simple Guide

To get alert text in Selenium, first switch to the alert using driver.switchTo().alert(), then call getText() on the alert object. This returns the text message shown in the alert popup.
📐

Syntax

Use driver.switchTo().alert() to access the alert popup. Then call getText() on the alert object to retrieve the alert message text.

  • driver.switchTo(): Switches the context to a different frame or alert.
  • alert(): Selects the currently active alert popup.
  • getText(): Gets the text displayed in the alert.
java
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
💻

Example

This example shows how to open a webpage with a JavaScript alert, switch to the alert, get its text, print it, and then accept (close) the alert.

java
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AlertTextExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();

        driver.get("https://www.selenium.dev/documentation/webdriver/browser/alerts/");

        // Trigger alert by executing JavaScript
        ((org.openqa.selenium.JavascriptExecutor) driver).executeScript("alert('Hello from Selenium!');");

        // Switch to alert
        Alert alert = driver.switchTo().alert();

        // Get alert text
        String alertText = alert.getText();
        System.out.println("Alert text is: " + alertText);

        // Accept alert to close it
        alert.accept();

        driver.quit();
    }
}
Output
Alert text is: Hello from Selenium!
⚠️

Common Pitfalls

1. Not switching to alert before getting text: Trying to get alert text without switching causes NoAlertPresentException.

2. Alert not present yet: If alert appears after some delay, getText() fails. Use explicit waits to handle this.

3. Forgetting to accept or dismiss alert: Leaving alert open blocks further actions.

java
try {
    // Wrong: directly calling getText without switch
    String text = driver.findElement(By.id("alert")).getText(); // This will fail
} catch (Exception e) {
    System.out.println("Error: " + e.getMessage());
}

// Correct way
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
Output
Error: no such element: Unable to locate element: {"method":"id","selector":"alert"}
📊

Quick Reference

StepCode SnippetDescription
1Alert alert = driver.switchTo().alert();Switch to the alert popup
2String text = alert.getText();Get the alert message text
3alert.accept();Accept (close) the alert
4alert.dismiss();Dismiss the alert (if cancel option)

Key Takeaways

Always switch to the alert using driver.switchTo().alert() before accessing its text.
Use getText() on the alert object to retrieve the alert message.
Handle alerts promptly by accepting or dismissing to avoid blocking further actions.
Use explicit waits if alerts appear after some delay to avoid exceptions.
Do not try to get alert text using normal element locators; alerts are separate browser popups.