Handle Alerts

Alert is a small message or notification box that appears on the display screen to notifies the user about some information or seeks permission to perform certain operations. It is also used for giving warning about some kind of risk or damaging operations.


Types of Alert

It is mainly classified into 3 categories:

1. Simple Alert

This alert is used for notifying some simple information or giving warning

Simple Alert Example

2. Prompt Alert

This alert accepts some inputs from the user

Prompt Alert Example

3. Confirmation Alert

This alert seeks permission from the user to perform some kind of operation.

Confirmation Alert Example


Handle Alerts in Selenium Webdriver

Webdriver provides below four methods to handle alerts using Alert Interface.

1. void dismiss()

This button clicks on the cancel button of the popup

Syntax: driver.switchTo().alert().dismiss(); 

2. void accept()

This button clicks on the accept button of the popup

Syntax: driver.switchTo().alert().accept(); 

3. String getText()

This method captures the text displays on the popup

Syntax: driver.switchTo().alert().accept(); 

4. sendKeys()

This method enters some data into the popup

Syntax: driver.switchTo().alert().sendKeys(“Data”);


Let’s practice automating these alerts into our site https://demoapp1.tequality.tech/

Step1: Click on ‘Launch Modal’ button
Step2: Switch to alert popup and provide username in the field
Step3: Click on Ok button

Handle Alerts using Selenium Webdriver Example

//click on the launch modal button

// send text in the alert box

//accept the alert

Practice yourself: Try automating below steps by yourself

Step1: Click on button
Step2: Switch to alert popup and read the text
Step3: Click on Cancel button

Refer above for syntax to read text and dismiss popup window.