- Selenium Tutorial
- Selenium - Home
- Selenium - Overview
- Selenium - Components
- Selenium - Automation Testing
- Selenium - Environment Setup
- Selenium - Remote Control
- Selenium IDE Tutorial
- Selenium - IDE Introduction
- Selenium - Features
- Selenium - Limitations
- Selenium - Installation
- Selenium - Creating Tests
- Selenium - Creating Script
- Selenium - Store Variables
- Selenium - Alerts & Popups
- Selenium - Selenese Commands
- Selenium - Actions Commands
- Selenium - Accessors Commands
- Selenium - Assertions Commands
- Selenium - Assert/Verify Methods
- Selenium - Locating Strategies
- Selenium - Script Debugging
- Selenium - Verification Points
- Selenium - Pattern Matching
- Selenium - JSON Data File
- Selenium - Browser Execution
- Selenium - User Extensions
- Selenium - Code Export
- Selenium - JavaScript Functions
- Selenium - Plugins
- Selenium WebDriver Tutorial
- Selenium - Introduction
- Selenium WebDriver vs RC
- Selenium - Installation
- Selenium - First Test Script
- Selenium - Driver Sessions
- Selenium - Browser Options
- Selenium - Chrome Options
- Selenium - Edge Options
- Selenium - Firefox Options
- Selenium - Safari Options
- Selenium Web Elements
- Selenium - File Upload
- Selenium - Locator Strategies
- Selenium - Relative Locators
- Selenium - Finders
- Selenium - Find All Links
- Selenium - User Interactions
- Selenium - WebElement Commands
- Selenium - Browser Interactions
- Selenium - Browser Commands
- Selenium - Browser Navigation
- Selenium - Alerts & Popups
- Selenium - Handling Forms
- Selenium - Windows and Tabs
- Selenium - Handling Links
- Selenium - Input Boxes
- Selenium - Radio Button
- Selenium - Checkboxes
- Selenium - Dropdown Box
- Selenium - Handling IFrames
- Selenium - Handling Cookies
- Selenium - Date Time Picker
- Selenium - Dynamic Web Tables
- Selenium - Actions Class
- Selenium - Keyboard Events
- Selenium - Key Up/Down
- Selenium - Copy and Paste
- Selenium - Handle Special Keys
- Selenium - Mouse Events
- Selenium - Drag and Drop
- Selenium - Scroll Operations
- Selenium - Waiting Strategies
- Selenium - Explicit/Implicit Wait
- Selenium - Support Features
- Selenium - Multi Select
- Selenium - Wait Support
- Selenium - Color Support
- Selenium - Errors & Logging
- Selenium - Logging
- Selenium - Exception Handling
- Selenium - Miscellaneous
- Selenium - Handling Ajax Calls
- Selenium - JSON Data File
- Selenium - CSV Data File
- Selenium - Excel Data File
- Selenium - Cross Browser Testing
- Selenium - Multi Browser Testing
- Selenium - Multi Windows Testing
- Selenium - JavaScript Executor
- Selenium - Headless Execution
- Selenium - Capture Screenshots
- Selenium - Capture Videos
- Selenium - Page Object Model
- Selenium - Page Factory
- Selenium - Record & Playback
- Selenium - Frameworks
- Selenium - Browsing Context
- Selenium Grid Tutorial
- Selenium - Overview
- Selenium - Architecture
- Selenium - Components
- Selenium - Configuration
- Selenium - Create Test Script
- Selenium - Test Execution
- Selenium - Endpoints
- Selenium - Customizing a Node
- Selenium Reporting Tools
- Selenium - Reporting Tools
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & other Technologies
- Selenium - Java Tutorial
- Selenium - Python Tutorial
- Selenium - C# Tutorial
- Selenium - Javascript Tutorial
- Selenium - Kotlin Tutorial
- Selenium - Ruby Tutorial
- Selenium - Maven & Jenkins
- Selenium - Database Testing
- Selenium - LogExpert Logging
- Selenium - Log4j Logging
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash Testing
- Selenium - Apache Ant
- Selenium - Github
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium Miscellaneous Concepts
- Selenium - Automation Frameworks
- Selenium - Keyword Driven Framework
- Selenium - Data Driven Framework
- Selenium - Hybrid Driven Framework
- Selenium - SSL Certificate Error
- Selenium - Alternatives
- Selenium Useful Resources
- Selenium - Quick Guide
- Selenium - Useful Resources
- Selenium - Automation Practice
- Selenium - Discussion
Selenium WebDriver - Alerts & Popups
Selenium Webdriver can be used to handle alerts and popups. An alert on a web page is designed to show a warning message, or information, or to get the user authorization to proceed for further actions.
There are multiple methods available in Selenium that can enable us to automate tests based on alerts and popups. For accessing the alerts, we have to first switch the driver context to the alert using the switchTo().alert() method. Let us discuss in details some of the methods of the Alert interface −
driver.switchTo().alert().accept() − This is used to accept an alert by clicking on the Ok button appearing on an alert.
driver.switchTo().alert().dismiss() − This is used to dismiss an alert by clicking on the Cancel button appearing on an alert.
driver.switchTo().alert().getText() − This is used to get the text appearing on an alert.
driver.switchTo().alert().sendKeys("value to be entered") − This is used to enter some text appearing on an input text appearing on an alert.
Please note that, while using the Alert interface, we would need to add the import statement import org.openqa.selenium.Alert in our tests.
Let us discuss a normal alert on a web page. On clicking the Alert button on the page, an alert would be generated with the text - Hello world! as shown in the below image.
Syntax
Syntax on Normal alert −
// switch driver context to alert Alert alrt = driver.switchTo().alert(); // Get alert text String s = alrt.getText(); System.out.println("Alert text on prompt is: " + s); // accept alert alrt.accept();
Example
Code Implementation on Alrts.java class file.
package org.example; 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 java.util.concurrent.TimeUnit; public class Alrts { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 12 secs driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); // Opening the webpage where we will get alert driver.get("https://www.tutorialspoint.com/selenium/practice/alerts.php"); // identify button for clicking to get alert WebElement c = driver.findElement(By.xpath("//button[text()='Alert']")); c.click(); // switch driver context to alert Alert alrt = driver.switchTo().alert(); // dismiss alert alrt.dismiss(); //again get the alert c.click(); // Get alert text String s = alrt.getText(); System.out.println("Alert text is: " + s); // accept alert alrt.accept(); // quitting the browser driver.quit(); } }
Output
Alert text is: Hello world! Process finished with exit code 0
In the above example, we captured the text on the alert and received the message in the console - Alert text is: Hello world!.
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Let us discuss another alert on a web page as shown in the below image. On clicking the second Click Me button, we would get an alert with the text - Press a button! on the web page.
On clicking the OK button on the alert, we would get the text You pressed OK! on the web page.
And on clicking the Cancel button on the alert, we would get the text You pressed Cancel! on the page.
Syntax
Syntax on Confirmation Alert −
Alert alrt = driver.switchTo().alert(); // Get alert text String s = alrt.getText(); System.out.println("Alert text t is: " + s); //accept alert alrt.accept(); // again get the alert and switch to it Alert alrt1 = driver.switchTo().alert(); // dismiss alert alrt1.dismiss();
Example
Code Implementation on AlertsJS.java class file.
package org.example; 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 java.util.concurrent.TimeUnit; public class AlertJS { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 12 secs driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); // Opening the webpage where we will get alert driver.get("https://www.tutorialspoint.com/selenium/practice/alerts.php"); // identify button for clicking to get alert WebElement c =driver.findElement (By.xpath("/html/body/main/div/div/div[2]/div[3]/button")); c.click(); // switch driver context to alert Alert alrt = driver.switchTo().alert(); // Get alert text String s = alrt.getText(); System.out.println("Alert text is: " + s); //accept alert alrt.accept(); // get text after accepting alert WebElement text =driver.findElement(By.xpath("//*[@id='desk']")); System.out.println("Text appearing after alert accept: " + text.getText()); // again get the alert c.click(); // switch driver context to alert Alert alrt1 = driver.switchTo().alert(); // now dismiss alert alrt1.dismiss(); // get text after dismissing alert WebElement text1 =driver.findElement(By.xpath("//*[@id='desk']")); System.out.println("Text appearing after alert dismiss: " + text1.getText()); // quitting the browser driver.quit(); } }
Output
Alert text is: Press a button! Text appearing after alert accept: You pressed OK! Text appearing after alert dismiss: You pressed Cancel! Process finished with exit code 0
In the above example, we captured the text on the alert and received the message in the console - Alert text is: Press a button!. Then on accepting the alert, we had captured a text which appeared on the page with the message appearing on the console - Text appearing after alert accept: You pressed OK!. Also, on dismissing the alert, we had captured a text which appeared on the page with the message appearing on the console - Text appearing after alert dismiss: You pressed Cancel!.
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Let us discuss another alert on a web page as shown in the below image. On clicking the third Click Me button, we would get an alert with the text - What is your name? along with an input box on the web page.
Syntax
Syntax on Prompt Alert −
// switch driver context to alert Alert alrt = driver.switchTo().alert(); // Get alert text String s = alrt.getText(); System.out.println("Alert text is: " + s); // enter text then accept alert alrt.sendKeys("Selenium"); alrt.accept(); // again get the alert and switch to it Alert alrt1 = driver.switchTo().alert(); // enter text then dismiss alert alrt1.sendKeys("Selenium"); alrt1.dismiss();
Example
Code Implementation on AlertsPromp.java class file.
package org.example; 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 java.util.concurrent.TimeUnit; public class AlertsPromp { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 12 secs driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); // Opening the webpage where we will get alert driver.get("https://www.tutorialspoint.com/selenium/practice/alerts.php"); // identify button for clicking to get alert WebElement c =driver.findElement (By.xpath("/html/body/main/div/div/div[2]/div[4]/button")); c.click(); // switch driver context to alert Alert alrt = driver.switchTo().alert(); // Get alert text String s = alrt.getText(); System.out.println("Alert text is: " + s); // enter text then accept alert alrt.sendKeys("Selenium"); alrt.accept(); // again get the alert c.click(); // switch driver context to alert Alert alrt1 = driver.switchTo().alert(); // again enter text then dismiss alert alrt1.sendKeys("Selenium"); alrt1.dismiss(); // quitting the browser driver.quit(); } }
Output
Alert text is: What is your name? Process finished with exit code 0
In the above example, we captured the text on the alert and received the message in the console - Alert text is: What is your name?
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Let us discuss another alert on a web page as shown in the below image. On clicking the first Click Me button, we would get an alert with the text - Hello just appeared which would appear after 5 seconds.
Syntax
// switch driver context to alert Alert alrt = driver.switchTo().alert(); // explicit wait for 6 secs expected condition for presence alert WebDriverWait wt = new WebDriverWait(driver, Duration.ofSeconds(6)); wt.until(ExpectedConditions.alertIsPresent()); // Get alert text String s = alrt.getText(); System.out.println("Alert text is: " + s); // alert accept alrt.accept();
Example
Code Implementation on AlertsPromp.java class file.
package org.example; 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.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; public class AlertsPromp { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // Opening the webpage where we will get alert driver.get("https://www.tutorialspoint.com/selenium/practice/alerts.php"); // identify button for clicking to get alert WebElement c = driver.findElement (By.xpath("/html/body/main/div/div/div[2]/div[2]/button")); c.click(); // explicit wait to expected condition for presence alert WebDriverWait wt = new WebDriverWait(driver, Duration.ofSeconds(6)); wt.until(ExpectedConditions.alertIsPresent()); // switch driver context to alert Alert alrt = driver.switchTo().alert(); // Get alert text String s = alrt.getText(); System.out.println("Text is: " + s); // accept alert alrt.accept(); // quitting the browser driver.quit(); } }
Output
Text is: Hello just appeared Process finished with exit code 0
Thus, in this tutorial, we had discussed how to handle various kinds of alerts and pop-ups using the Selenium Webdriver.
To Continue Learning Please Login
Login with Google