Alerts

In Selenium, an alert refers to a pop-up dialog box that is part of a web page and is typically used to convey important information, prompt the user for input, or confirm an action. Handling alerts is an essential aspect of web automation, and Selenium provides methods to interact with and manipulate these pop-ups.

Following are the different types of alerts:

Simple Alert

Simple alert presents a message to the user and usually has an OK button which is used to close this alert box to proceed. Click here to see the simple alert.
In the following example we have used driver.switchTo().alert() method to switch to an alert window. Then we have used getText() method to get the text on the alert. We have used accept() method to accept the message and close it.
Java
C#
Python
Javascript
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/misc/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));

driver.findElement(By.id("dialog1")).click();
// Switch to alert
Alert alert = driver.switchTo().alert();
// Get the alert text
String alertText = alert.getText();
System.out.println(alertText);
// Accept the alert
alert.accept();

driver.quit();
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/misc/";
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
driver.Manage().Window.Maximize();
driver.FindElement(By.Id("dialog1")).Click();
IAlert alert = driver.SwitchTo().Alert();
// Get the alert text
String alertText = alert.Text;
Console.WriteLine(alertText);
// Accept the alert
alert.Accept();
driver.Quit();
Python code coming soon
Javascript code coming soon
Kotlin code coming soon

Confirmation Alert

The confirm box is often used to asks the user for confirmation with OK and Cancel buttons. When a confirm box displayed, the user will have to click either OK or Cancel button to close and proceed. These two buttons returns two different response from user. If the user click on OK, the box returns response as true. If the user clicks on Cancel, the box returns response as false. Like simple alert, this is also captured using driver.switchTo().alert(). Additionally you have alert.dismiss() to cancel the confirmation alert by registering the response as not accepted. Click here to see confirm alert

Below is an example of confirmation alert.

Java
C#
Python
Javascript
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/misc/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
driver.findElement(By.id("dialog2")).click();
// Switch to alert
Alert alert = driver.switchTo().alert();
// Get the alert text
String alertText = alert.getText();
System.out.println(alertText);
// Cancel the alert
alert.dismiss();
driver.quit();
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/misc/";
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
driver.Manage().Window.Maximize();
driver.FindElement(By.Id("dialog2")).Click();
IAlert alert = driver.SwitchTo().Alert();
// Get the alert text
String alertText = alert.Text;
Console.WriteLine(alertText);
// Cancel the alert
alert.Dismiss();
driver.Quit();
Python code coming soon
Javascript code coming soon
Kotlin code coming soon

Prompt Alert

Similar to a confirmation alert but it includes a text input field. This is used to get some information from user. When a prompt box is displayed, the user will have to click either on OK or Cancel button to proceed after entering the requested input value. If the user click on OK button the prompt box returns the input value entered by user to server. If the user click on Cancel button the prompt box returns null value. Like confirmation alert, it is also captured using driver.switchTo().alert(). Additionally we have sendKeys() method to enter user input in the prompt text box. Click here to see prompt alert.

Below is an example to handle prompt alert.

Java
C#
Python
Javascript
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/misc/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
driver.findElement(By.id("dialog3")).click();
// Switch to alert
Alert alert = driver.switchTo().alert();
// Get the alert text
String alertText = alert.getText();
System.out.println(alertText);
//Enter text in prompt text box
alert.sendKeys("Welcome");
// Accept the alert
alert.accept();
driver.quit();
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/misc/";
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
driver.Manage().Window.Maximize();
driver.FindElement(By.Id("dialog3")).Click();
IAlert alert = driver.SwitchTo().Alert();
//Enter text in prompt text box
alert.SendKeys("Welcome");
// Accept the alert
alert.Accept();
driver.Quit();
Python code coming soon
Javascript code coming soon
Kotlin code coming soon