Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

Handling alerts and pop-ups in Selenium WebDriver using Java involves several methods and classes. Here’s a detailed explanation with examples.

Types of Alerts

  1. Simple Alert: Contains a message and an OK button.
  2. Confirmation Alert: Contains a message, an OK button, and a Cancel button.
  3. Prompt Alert: Contains a message, an input field, an OK button, and a Cancel button.

Handling Alerts

1. Handling a Simple Alert

A simple alert can be handled by switching to the alert and accepting it.

Example:

				
					console.log( 'Code is Poetry' );import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class HandleSimpleAlert {
    public static void main(String[] args) {
        // Set up the WebDriver and launch the browser
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();

        // Open the web page with the alert
        driver.get("https://example.com/simple-alert");

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

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

        // Accept the alert
        alert.accept();

        // Close the browser
        driver.quit();
    }
}

				
			

2. Handling a Confirmation Alert

A confirmation alert can be handled by switching to the alert and either accepting or dismissing it.

Example:

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

public class HandleConfirmationAlert {
    public static void main(String[] args) {
        // Set up the WebDriver and launch the browser
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();

        // Open the web page with the alert
        driver.get("https://example.com/confirmation-alert");

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

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

        // Accept the alert
        alert.accept();

        // Or to dismiss the alert
        // alert.dismiss();

        // Close the browser
        driver.quit();
    }
}

				
			

3. Handling a Prompt Alert

A prompt alert can be handled by switching to the alert, sending text to it, and then accepting or dismissing it.

Example:

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

public class HandlePromptAlert {
    public static void main(String[] args) {
        // Set up the WebDriver and launch the browser
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();

        // Open the web page with the alert
        driver.get("https://example.com/prompt-alert");

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

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

        // Send text to the alert
        alert.sendKeys("Some text");

        // Accept the alert
        alert.accept();

        // Or to dismiss the alert
        // alert.dismiss();

        // Close the browser
        driver.quit();
    }
}

				
			

Handling Browser Pop-ups

Using getWindowHandles and getWindowHandle

When a new browser window or tab is opened, Selenium WebDriver provides methods to handle multiple windows.

Example:

				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;

public class HandlePopUp {
    public static void main(String[] args) {
        // Set up the WebDriver and launch the browser
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();

        // Open the web page
        driver.get("https://example.com");

        // Store the current window handle
        String mainWindowHandle = driver.getWindowHandle();

        // Assume a new window is opened here
        // Click a link or button that opens a new window/tab

        // Get all window handles
        Set<String> allWindowHandles = driver.getWindowHandles();

        // Switch to the new window handle
        for (String handle : allWindowHandles) {
            if (!handle.equals(mainWindowHandle)) {
                driver.switchTo().window(handle);
                break;
            }
        }

        // Perform operations in the new window
        // ...

        // Close the new window
        driver.close();

        // Switch back to the main window
        driver.switchTo().window(mainWindowHandle);

        // Close the browser
        driver.quit();
    }
}

				
			

Handling alerts and pop-ups in Selenium involves using the Alert interface for JavaScript alerts and managing multiple window handles for browser pop-ups. By understanding these methods and applying them correctly, you can effectively manage various types of alerts and pop-ups in your web automation tasks.

Scroll to Top