Home » selenium-tutorial » Unexpected popup window handling using selenium

Newly Updated Posts

Unexpected popup window handling using selenium

In this tutorial we will learn unexpected popup window handling using selenium. Also we will learn how to handle multiple window and its window id as per the requirement of automation script.

How to resolve Unexpected popup window issue using Selenium?

In a web application we sometimes come across an issue where we get unexpected popup windows. We see other windows open when we try opening single application URL. In such cases it becomes tough to work with main window and sometimes our automation scripts gets stuck or failed.

To overcome this Unexpected issue we handle it in our automation script so that automation script runs smoothly without any hurdles.

The solution to overcome with such unexpected popup windows is to get window ids of unexpected window and take the control to that window ids and close them using selenium.

Consider the below scenario:

  • Open the web application of online job portal https://www.naukri.com/
  • After it opens, it opens with two other window.
  • Close both the other window
  • Now take the control of driver to main page
  • print the title of main page

Complete Code of the above scenario.

package SeleniumArchitecture;
import java.util.Base64;
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.chrome.ChromeDriver;
public class UnExpectedPopUpWindow_Handle {

public static void main(String[] args) throws InterruptedException {

System.setProperty( "webdriver.chrome.driver","C:\\Users\\XXXXX\\Desktop\\Eclipse_Installer\\GeckoDriver\\chromedriver.exe");	  
// Create oject of chrome driver WebDriver driver = new ChromeDriver();
 Thread.sleep(3000);       
 ChromeDriver    driver= new ChromeDriver();	  
 driver.manage().window().maximize(); Thread.sleep(3000);
 driver.get("https://www.naukri.com/");
		  
 Set<String> windowIds=driver.getWindowHandles();
			 
 System.out.println("Total windows------------>"+windowIds.size());
			 
 Iterator<String> itera= windowIds.iterator();
 if(windowIds.size()==3) {					  
 String MainWindowID = itera.next();
 String secondWindowID = itera.next();
 String thirdWindowID = itera.next();
			
 driver.switchTo().window(secondWindowID);
 driver.close();
 driver.switchTo().window(thirdWindowID);
 driver.close();	
 driver.switchTo().window(MainWindowID);		
 String mainWindowTitle = driver.getTitle();
System.out.println("Title for the main window is ------ >"+mainWindowTitle);			  
		  }
	}
}

Code explanation:

  • In selenium, we can capture the window ids of multiple windows using driver.getWindowHandles(), it returns a set and add all the window ids present in the current driver instance.

code -> Set windowIds=driver.getWindowHandles();

  • Now its time to iterate the window ids for this we call iterator, in set we iterate using iterator()

code-> Iterator itera= windowIds.iterator();

  • Now apply an if statement having condition that window ids are equal to 3 (note we have 3 window appearing to this application) and click to next till iteration to the window ids which need to be closed. and on pointing to that close the driver and then switch to main window

Code->

if(windowIds.size()==3) {

String MainWindowID = itera.next();
String secondWindowID = itera.next();
String thirdWindowID = itera.next();
driver.switchTo().window(secondWindowID);
driver.close();
driver.switchTo().window(thirdWindowID);
driver.close();
driver.switchTo().window(MainWindowID);
 String mainWindowTitle = driver.getTitle();

 System.out.println("Title for the main window is --------------->"+mainWindowTitle);
      }

So till now we have learned how unexpected popup issue is handled using selenium. In next topic we will learn more such topics related to selenium web-driver.