Handling Alerts in Selenium
                            
package org.openqa.selenium;
                  
public interface Alert {
  void dismiss();
  void accept();
  String getText();
  void sendKeys(String keysToSend);
}
                            
                          
                        Window alert()
Definition and Usage
The alert() method displays an alert box with a message and an OK button.
The alert() method is used when you want information to come through to the user.
Note
The alert box takes the focus away from the current window, and forces the user to read the message.
Do not overuse this method. It prevents the user from accessing other parts of the page until the alert box is closed.
                        
                            
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
  
public class AlertExample1 {
  public static void main(String[] args) throws Exception {
    WebDriverManager.chromedriver().setup();
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert");
    driver.switchTo().frame("iframeResult");
    Thread.sleep(2000);
    driver.findElement(By.xpath("//button[text()='Try it']")).click();
    Thread.sleep(2000);
    driver.switchTo().alert().accept();
    Thread.sleep(2000);
    driver.quit();
  }
}
                              
                          
                        Window confirm()
The confirm() method displays a dialog box with a message, an OK button, and a Cancel button. The confirm() method returns true if the user clicked "OK", otherwise false.
Note
A confirm box is often used if you want the user to verify or accept something. A confirm box takes the focus away from the current window, and forces the user to read the message. Do not overuse this method. It prevents the user from accessing other parts of the page until the box is closed.
                        
                                
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
  
public class AlertExample1 {
  public static void main(String[] args) throws Exception {
    WebDriverManager.chromedriver().setup();
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_confirm3");
    driver.switchTo().frame("iframeResult");
    Thread.sleep(2000);
    driver.findElement(By.xpath("//button[text()='Try it']")).click();
    Thread.sleep(2000);
    driver.switchTo().alert().accept();
    Thread.sleep(2000);
    driver.findElement(By.xpath("//button[text()='Try it']")).click();
    Thread.sleep(2000);
    driver.switchTo().alert().dismiss();
    Thread.sleep(2000);
    driver.quit();
  }
}
                                
                              
                        Window prompt()
Definition and Usage
The prompt() method displays a dialog box that prompts the user for input. The prompt() method returns the input value if the user clicks "OK", otherwise it returns null.
Note
A prompt box is used if you want the user to input a value. When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed. Do not overuse this method. It prevents the user from accessing other parts of the page until the box is closed.
                        
                                    
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
  
public class AlertExample3 {
  public static void main(String[] args) throws Exception {
    WebDriverManager.chromedriver().setup();
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_prompt");
    driver.switchTo().frame("iframeResult");
    Thread.sleep(2000);
    driver.findElement(By.xpath("//button[text()='Try it']")).click();
    Thread.sleep(2000);
    System.out.println(driver.switchTo().alert().getText());
    driver.switchTo().alert().sendKeys("ranga swami reddy");
    Thread.sleep(2000);
    driver.switchTo().alert().accept();
    Thread.sleep(2000);
    driver.quit();
  }
}