Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Selenium Automation Testing

  • Introduction to Automation Testing
  •  Introduction to Selenium
  • Advantages and Disadvantages of Selenium WebDriver
  •  Locators in Selenium
  • How to launch the browsers ? 
  • Browser Navigation Commands in Selenium
  •  Handling the textbox and button using selenium
  • Handling the radio button and checkbox using selenium
  •  Handling the drop downs and multi-select dropdown
  • Validate the text validation and attributes values. 
  • Handling Alert Popup
  • Handling iFrames
  • Handling Multiple Browsers or Tabs
View Categories
  • Home
  • Tutorials
  • Selenium Automation
  • Selenium Automation Testing
  • Handling Alert Popup

Handling Alert Popup

 Handling Alert Popup

Explanation of key steps:

  1. Setup & launch – set the webdriver.chrome.driver system property and instantiate ChromeDriver.

2. Navigate – open the page that contains buttons to trigger various JavaScript alerts.

3. Trigger alerts – click the buttons that fire a simple alert, a confirmation dialog, and a prompt dialog.

4. Wait & switch – use WebDriverWait with ExpectedConditions.alertIsPresent() to pause until the alert appears, then driver.switchTo().alert() returns an Alert object.

5. Interact –

  • Simple alert: accept() to click “OK.”
  • Confirmation alert: dismiss() to click “Cancel.”
  • Prompt alert: sendKeys(…) to enter text, then accept().

6. Teardown – always wrap your logic in a try…finally and call driver.quit() to ensure the browser closes even if an exception occurs.

Adjust the URL, locators (By.xpath(…)), and button IDs (alertButton, confirmButton, promptButton) to match your application under test.

Program:

import org.openqa.selenium.Alert;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class AlertHandlingExample {

    public static void main(String[] args) throws InterruptedException {

        // 1. Point to your ChromeDriver executable

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        // 2. Launch Chrome

        WebDriver driver = new ChromeDriver();

        try {

            // 3. Navigate to the page with alerts

            driver.get("https://example.com/alerts");

            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));

            // --- SIMPLE ALERT ---

            // 4. Trigger a simple alert

            WebElement simpleAlertBtn = driver.findElement(

                By.xpath("//button[@id='alertButton']"));

            simpleAlertBtn.click();

            // 5. Wait for and switch to the alert

            Alert simpleAlert = wait.until(

                ExpectedConditions.alertIsPresent());

            // 6. Read and print its message

            System.out.println("Simple alert says: " + simpleAlert.getText());

            // 7. Accept the alert (click OK)

            simpleAlert.accept();

            Thread.sleep(1000);

            // --- CONFIRMATION ALERT ---

            // 8. Trigger a confirmation alert

            WebElement confirmAlertBtn = driver.findElement(

                By.xpath("//button[@id='confirmButton']"));

            confirmAlertBtn.click();

            Alert confirmAlert = wait.until(

                ExpectedConditions.alertIsPresent());

            System.out.println("Confirm alert says: " + confirmAlert.getText());

            // 9. Dismiss the alert (click Cancel)

            confirmAlert.dismiss();

            Thread.sleep(1000);

            // --- PROMPT ALERT ---

            // 10. Trigger a prompt alert

            WebElement promptAlertBtn = driver.findElement(

                By.xpath("//button[@id='promptButton']"));

            promptAlertBtn.click();

            Alert promptAlert = wait.until(

                ExpectedConditions.alertIsPresent());

            System.out.println("Prompt alert says: " + promptAlert.getText());

            // 11. Send text to the prompt and accept

            promptAlert.sendKeys("Selenium Test");

            promptAlert.accept();

            Thread.sleep(1000);

        } finally {

            // 12. Close the browser (end the session)

            driver.quit();

        }

    }

}

Selenium Automation Testing
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
Validate the text validation and attributes values. Handling iFrames
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105