Handle Pop-ups in Selenium Webdriver
Understanding what is Alert ?
Alert is a small message box which displays on-screen notification to give the user some kind of information or ask for permission to perform certain kind of operation. It may be also used for warning purpose.
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
There are two types of alerts that we would be focusing on majorly:
​
Windows based alert pop ups
Handling these pop-ups have always been a little tricky as we know Selenium is an automation testing tool which supports only web application testing, that means, it doesn’t support windows based applications and window alert is one of them. However Selenium alone can’t help the situation but along with some third party intervention, this problem can be overcome.
There are several third party tools available for handling window based pop-ups along with the selenium.
For Example:
Robot class is a java based utility which emulates the keyboard and mouse actions.
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Web based alert pop ups
It is a small box that appears on the display screen to give you some kind of information or to warn you about a potentially damaging operation or it may even ask you for the permissions for the operation.
​
Here are few alert types:
​
1) Simple Alert:- This simple alert displays some information or warning on the screen.
2) Prompt Alert:- This Prompt Alert ask some input from user and selenium webdriver can enter the text using sendkeys(" input…. ").
3) Confirmation Alert:- This confirmation alert asks permission to do some type of operation.
​
​
​
​
​
​
​
​
​
​
​
​
​
How to handle Alert in Selenium WebDriver
Alert interface provides the below few methods which are widely used in Selenium Webdriver.
​
1) Using void dismiss() // To click on the 'Cancel' button of the alert.
driver.switchTo().alert().dismiss();
​
2) void accept() // To click on the 'OK' button of the alert.
driver.switchTo().alert().accept();
​
3) String getText() // To capture the alert message.
driver.switchTo().alert().getText();
​
4) void sendKeys(String stringToSend) // To send some data to alert box.
driver.switchTo().alert().sendKeys("Text");
​
You can see a number of Alert methods are displayed as shown in below screen suggested by Eclipse.
We can easily switch to alert from the main window by using Selenium's .switchTo() method.
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Alert_Demo {
public static void main(String[] args) throws NoAlertPresentException {
WebDriver driver = new FirefoxDriver();
// Alert Message handling
driver.get("http://demo.guru99.com/V4/");
driver.findElement(By.name("uid")).sendKeys("mngr30127");
driver.findElement(By.name("password")).sendKeys("EzAtAqy");
driver.findElement(By.name("btnLogin")).submit();
driver.findElement(By.linkText("Delete Customer")).click();
driver.findElement(By.name("cusid")).sendKeys("53920");
driver.findElement(By.name("AccSubmit")).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);
// Accepting alert
alert.accept();
}
}
​
Using Window Handler To Switch To And Fro From Pop-up window
​
To switch to a popup window, you need to use getWindowHandles() and iterate through them.
​
In your code you are using getWindowHandle() which will give you the parent window itself.
// Store your parent window
String parentWindowHandler = driver.getWindowHandle();
String subWindowHandler = null;
// get all window handles
Set<String> handles = driver.getWindowHandles();
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
// switch to popup window
driver.switchTo().window(subWindowHandler);
// perform operations on popup
// switch back to parent window
driver.switchTo().window(parentWindowHandler);
How to handle multiple Selenium Popup window using Webdriver
​
In automation, when we have multiple windows in any web application, the activity may need to switch control among several window from one to other in order to complete the operation. After completion of the operation, it has to return to the main window i.e. parent window. We will see this further in the article with an example.
​
In selenium web driver there are methods through which we can handle multiple windows.
Driver.getWindowHandles();
​
To handle all opened windows by web driver, we can use "Driver.getWindowHandles()" and then we can switch window from one window to another in a web application. Its return type is Iterator<String>.
​
Driver.getWindowHandle();
​
When the site opens, we need to handle the main window by driver.getWindowHandle(). This will handle the current window that uniquely identifies it within this driver instance. Its return type is String.
To handle multiple windows in Selenium WebDriver, we follow the following steps.
Now, we will automate the given below scenario to see how to handle multiple windows using Selenium Webdriver.
We asssume here that site "xyz.com" has popups.
​
Step 1) Launch the site.
Launch the browser and open the site " http://xyz.com "
​
Step 2) Click on link "Click Here ".
When the user clicks on the " Click Here " link, new child window opens.
Step 3) New Child Window opens.
New window opens, ask the user to enter email id and submit the page.
Step 4) Enter your email ID and submit.
Step 5) Display the Access Credentials on submitting the page.
When you execute the code, you will see the child window is open in new tab.
Step 6) Close the Child window on which credentials are displayed.
Step 7) Switch to the parent window.
Handling multiple window in selenium webdriver.
​
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
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("http://xyz.com/popup.php");
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("myname@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);
}
}
Output:
​
When you execute the above code, it launches the site and on clicking the link "Click here," it opens up a child window in a new tab. You can close the child window, and switch to the parent window once the operation is completely done.
Hence handling more than one window in the application.


