
In this tutorial, we will learn How to handle Alerts/Pop-Ups in Selenium web driver automation.
Alerts:
Alert is a notification message box that displays options to act on it or it will give some kind of information. It may be also used for warning pop-ups when you do specific action on a web page.
How to identify Alerts:
Users cannot do any further actions on the web page until you act on it and you cannot inspect it.
Types of Alerts:
- Simple Alert:
In a simple alert, it will display some information or warning and Ok button to click on it.

2.Prompt Alert:
In a prompt alert, it will ask some information to enter data in text field along with ‘Ok’ and ‘Cancel’ button

3.Confirmation Alert:
In confirmation alert, it will ask permission to act on it.

Whenever we get any alerts in a web-based application then we cannot do any action on a web page and we need to handle these alerts to run the test case smoothly. To handle alerts we have to click options present in alerts.
To handle alerts need to initialize alert interface and using alert methods we can handle alerts.
Alert alrt=driver.switchTo().alert();
i. dismiss(): By using this method we can click on the cancel button
alrt.dismiss();
ii. accept(): By using this method we can click on OK button
alrt.accept();
iii. getText(): By using this method we can retrieve alert message
alrt.getText();
iv. sendKeys(): By using this method we can enter data into alert prompt
alrt.sendKeys(“data”);
Please find the below sample script to know how to handle Alerts/Pop-Ups in Selenium
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import java.util.concurrent.TimeUnit; 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.testng.annotations.Test; public class HandlingAlerts { @Test public void HandlingAlerts() { //Initialize reference to driver WebDriver driver; //Setting the properties for chromedriver System.setProperty("webdriver.chrome.driver","./drivers/chromedriver.exe"); //Initialize the chromedriver for webdriver reference driver=new ChromeDriver(); //Launching application driver.get("https://automationexplore.com/selenium-automation-practice-page/"); //Setting implicit wait to load all web elements in application driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); //Maximize the window driver.manage().window().maximize(); //Click the button to get alert popup driver.findElement(By.xpath("//button[@id='alertbutton']")).click(); //Handling alert //Initialize alert interface and switch to alert Alert alt=driver.switchTo().alert(); //To click OK button alt.accept(); //Click the button to get alert popup driver.findElement(By.xpath("//button[@id='alertbutton']")).click(); //To click cancel button alt.dismiss(); } } |
Note: I have used the chrome driver. if you want to download the driver you can get from selenium official website or simply you can click here.
To inspect and practice sample automation web page you can click here
Please provide your comments or you need more info on this. Thank you ?.