Alerts and Popups in Selenium

The next topic that we need to discuss in the selenium testing tutorial is Alert handling. And now we will initially start with

what is an Alert()?

An alert is a small message box that displays an on-screen notification. This gives the user some kind of information (or) ask permission for certain kind of options. An alert usually gives a warning to the user.

Alerts are classified into the following types:

Simple Alert():

A simple Alert displays some information (or) warning on the screen.

alerts and popups in selenium

Prompt alert():

A prompt alert usually asks some input from the user and selenium web driver can enter the text using send keys (“input”)

 alerts and popups in selenium 

Confirmation alert():

A confirmation alert asks some input from the user to do some kind of operation

alerts and popups in selenium

In learn selenium online we will discuss

How to handle alerts in Selenium Webdriver?

Selenium web driver provides an alert () interface that contains the following methods

Method name Function Description
Void dismiss() driver.switchTo().alert().dismiss(); This is used to cancel the alert button
Void accept() driver.switchTo().alert().alert(); This is used to click OK button of the alert()
StringgetText() driver.switchTo().alert().getText(); It is used to capture the alert message
Void sendKeys() driver.switchTo().alert().sendkeys(“text”); It is used to send some data to the alert box

We can implement these methods in Selenium web driver using the following code

import org.openqa.selenium.By;

//import web drivers
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

// import exception classes
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.Alert;

public class AlertDemo {

public static void main(String[] args) throws NoAlertPresentException,InterruptedException {
System.setProperty("webdriver.chrome.driver","G:\chromedriver.exe");
WebDriver driver = new ChromeDriver();

// Alert Message handling

driver.get("https://onlineitguru.com/onlinetutorials/selenium");

driver.findElement(By.name("cusid")).sendKeys("53920");
driver.findElement(By.name("submit")).submit();

// Switching to Alert
Alert alert = driver.switchTo().alert();

// Capturing alert message.
String alertMessage= driver.switchTo().alert().getText();

// Displaying alert message
System.out.println(alertMessage);
Thread.sleep(5000);

// Accepting alert
alert.accept();
}

}

And the next topic in Selenium training is Pop- up

what is a pop- up?

It is an ability to interact with multiple windows including alerts.  This allows switching control to pop-up while keeping the browser in the background. And soon after the operation completion, this would return to the main window.

How to handle Selenium pop-ups using Selenium Web driver?

it training says the following methods can handle multiple windows

Driver.getWindowHandles():

This method is used to handle all the web driver, browser windows. In a web application, this allows you to switch from one window to the other. It usually returns the string Iterator.

Driver. getWindowHandle():

When the site is opened this method is used to handle the main window. This will usually handle the current window that uniquely identifies within the driver instance.

And by executing the following code, we handle the Selenium popp-ups:

import java.util.Iterator;
import java.util.Set;
//import Selenium packages
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
// import borwser drivers
import org.openqa.selenium.firefox.FirefoxDriver;

public class WindowHandle_Demo {

public static void main(String[] args) throws InterruptedException {
WebDriver driver=new FirefoxDriver();

//Launching the site.
driver.get("https://onlineitguru.com/onlinetutorials/selenium");
driver.manage().window().maximize();

driver.findElement(By.xpath("//*[contains(@href,'popup.php')]")).click();

String MainWindow=driver.getWindowHandle();

// To handle all new opened window.
Set<String> s1=driver.getWindowHandles();
Iterator<String> i1=s1.iterator();

while(i1.hasNext())
{
String ChildWindow=i1.next();

if(!MainWindow.equalsIgnoreCase(ChildWindow))
{

// Switching to Child window
driver.switchTo().window(ChildWindow);
driver.findElement(By.name("emailid"))
.sendKeys("gaurav.3n@gmail.com");

driver.findElement(By.name("btnLogin")).click();

// Closing the Child Window.
driver.close();
}
}
// Switching to Parent window i.e Main Window.
driver.switchTo().window(MainWindow);
}
}

Example 2 :

Scenario:

In real time projects, there are multiple windows. And these multiple windows throws multiple pop-ups. And have you think about how to handle all these pop-ups? Strictly speaking, have you think about how to handle these multiple windows. If so, I come with a solution for you. \

Solution:

selenium Web driver has the capability to handle multiple windows using the Switch Case. Execute the following code in your Eclipse IDE to handle multiple pop-ups

package softwareTestingMaterial;
import org.testng.annotations.Test;

//import JAVA packages
import java.util.Iterator;
import java.util.Set;

//import web drivers

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MultipleWindowsClass{
@Test
public void testMultipleWindows() throws InterruptedException{
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"\src\test\java\drivers\geckodriver.exe");
// To open browser
WebDriver driver=new FirefoxDriver();
// To maximize browser
driver.manage().window().maximize();
// To open slenium tutorial with multiple windows
driver.get("https://onlineitguru.com/onlinetutorials/selenium");
// It will return the parent window name as a String
String mainWindow=driver.getWindowHandle();
// It returns no. of windows opened by WebDriver and will return Set of Strings
Set<String> set =driver.getWindowHandles();
// Using Iterator to iterate with in windows
Iterator<String> itr= set.iterator();
while(itr.hasNext()){
String childWindow=itr.next();
// Compare whether the main windows is not equal to child window. If not equal, we will close.
if(!mainWindow.equals(childWindow)){
driver.switchTo().window(childWindow);
System.out.println(driver.switchTo().window(childWindow).getTitle());
driver.close();
}
}
// This is to switch to the main window
driver.switchTo().window(mainWindow);
}
}

While  training at online educationmany people asked me what is the difference between  quit() and close()?

driver.quit()  is used to quit the whole browser session along with all the associated browser windows , tabs ad pop-ups.
driver.close() is used to close the current browser window. if there is only one browser open, it quick to whole browser session.

Subscribe
to our newsletter

Drop Us A Query

Trending Courses
  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • Deep learning course
    Deep Learning Course
  • Artificial Intelligence, Deep mastering (DL) is completely about, many levels of Representation and sort of abstraction. That guide to design a sense of Information like Images, sound and text format.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

  • devops with azure
    Devops with Azure Training
  • As we Know Azure DevOps is a Bunch of Services, in guiding Developers. It contains CI/CD, pipelines, code Repositories, Visual Reporting Tools and more code management with version control.


100% Secure Payments. All major credit & debit cards accepted.