Handling Window Pop Up In Selenium:
There may be a situation while doing automation where we need to handle multiple window popup or where we need to go to appropriate window among multiple windows at run time. Selenium code /automation get focused on default/parent window.
E.g If you navigate or go to Naukri.com url then you will get multiple window pop up only when if you haven't disable pop up option.
Here, How do we know that it is a window popup or java script pop up or Jquery/Normal pop up.
Window Pop up:
Window pop up has minimize, maximize and close button like any normal browser has.
driver.switchTo().window("windowName);
driver.switchTo().window();
Here driver referes to webDriver object -
WebDriver driver = new ChromeDriver();
Java Script Pop Up:
Java script pop up doesn't have minimize or maximize button/option also focus is only on java script pop up page. you can't ignore the java script popup but window pop up you can ignore it and perform operation on any other windows.
Ways to Handle Window Pop Up in Selenium:
Selenium webdriver assign unique id (alpha numeric ) to every window to identify uniquely. The alpha numeric id (unique id) is called "Window Handle" in selenium.
How to get Window Handle:
String handle = driver.getWindowHandle();
If there are multiple window gets opened when you click on any link,button etc... then to get "window Handle" of each window -
Set<String> windowHandles = driver.getWindowHandles();
"getWindowHandles()" : method return window handle (unique id for each opened window) so that the return type is set. Because set will not contain duplicate elements so here getWindowHandles return unique id/window handles for each winodw and stored in Set.
How to switch to correct / appropriate window:
Basically there are two ways to switch to appropriate window.
2. To get current/parent/default window handle:
String handle = driver.getWindowHandle();
driver.switchTo().window( handle );
or
Set < String > windowHandles = driver.getWindowHandles();
for(String handle : windowHandles )
{
driver.swicthTo().window( handle );
}
Example:
There may be a situation while doing automation where we need to handle multiple window popup or where we need to go to appropriate window among multiple windows at run time. Selenium code /automation get focused on default/parent window.
E.g If you navigate or go to Naukri.com url then you will get multiple window pop up only when if you haven't disable pop up option.
Here, How do we know that it is a window popup or java script pop up or Jquery/Normal pop up.
Window Pop up:
Window pop up has minimize, maximize and close button like any normal browser has.
driver.switchTo().window("windowName);
driver.switchTo().window();
Here driver referes to webDriver object -
WebDriver driver = new ChromeDriver();
Java Script Pop Up:
Java script pop up doesn't have minimize or maximize button/option also focus is only on java script pop up page. you can't ignore the java script popup but window pop up you can ignore it and perform operation on any other windows.
Ways to Handle Window Pop Up in Selenium:
Selenium webdriver assign unique id (alpha numeric ) to every window to identify uniquely. The alpha numeric id (unique id) is called "Window Handle" in selenium.
How to get Window Handle:
String handle = driver.getWindowHandle();
If there are multiple window gets opened when you click on any link,button etc... then to get "window Handle" of each window -
Set<String> windowHandles = driver.getWindowHandles();
"getWindowHandles()" : method return window handle (unique id for each opened window) so that the return type is set. Because set will not contain duplicate elements so here getWindowHandles return unique id/window handles for each winodw and stored in Set.
How to switch to correct / appropriate window:
Basically there are two ways to switch to appropriate window.
- By using windowName
2. To get current/parent/default window handle:
String handle = driver.getWindowHandle();
driver.switchTo().window( handle );
or
Set < String > windowHandles = driver.getWindowHandles();
for(String handle : windowHandles )
{
driver.swicthTo().window( handle );
}
Example:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package seleniumCode; | |
import java.util.List; | |
import java.util.Set; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
public class HandlingMultipleWindows { | |
public static void main(String[] args) throws InterruptedException | |
{ | |
WebDriver driver = null; | |
WebElement element = null; | |
List<WebElement> listOfElement = null; | |
//Initialize webdriver | |
System.setProperty("webdriver.chrome.driver", "C:\\Users\\apande\\Downloads\\chromedriverr\\chromedriver.exe"); | |
driver = new ChromeDriver(); | |
//Maximize window | |
driver.manage().window().maximize(); | |
//Navigate to naukri.com | |
driver.get("https://www.naukri.com/"); | |
//Get Parent Window Handle currently focus is on naukri.com parent | |
//window not child pop up window | |
String parentWindowHandle = driver.getWindowHandle(); | |
//Print Parent Window Handle / ID | |
System.out.println("Parent Window Handle: "+ parentWindowHandle); | |
// Find how many Windows (child windows) are opened under parent window | |
Set<String> windowHandles = driver.getWindowHandles(); | |
// Iterate through all handle and navigate to other window pop up except | |
// parent window | |
for (String windowHandle : windowHandles) | |
{ | |
// Print all Child Window Handle | |
System.out.println("Child Window Handle: "+ windowHandle); | |
if(!parentWindowHandle.equals(windowHandle)) | |
{ | |
// you can switch to all child window one-by-one and check for title or | |
// inspect any element from the landing page | |
// so that you will come to know you have landed on correct page. | |
driver = driver.switchTo().window(parentWindowHandle); | |
// Here I will check if img tag is there then I am considering I am | |
// landing on correct page | |
listOfElement = driver.findElements(By.tagName("img")); | |
if(listOfElement.size() > 0 ) | |
{ | |
System.out.println("I am landing on correct page !!"); | |
//Now perform your operation once all operation finishes then come | |
// to you parent page | |
// Here I will just click on image and I will print the navigate | |
// URL and switched to Parent Window | |
listOfElement.get(0).click(); | |
System.out.println("Child Window Title: "+driver.getTitle()); | |
System.out.println("New URL: "+driver.getCurrentUrl()); | |
//Now switch to parent window after finishing all task on child window | |
Thread.sleep(5000); | |
driver.switchTo().window(parentWindowHandle); | |
// Break the loop. No need to check other windows all windows | |
// are having unique handle/ID | |
break; | |
} | |
} | |
} | |
//Now you have landed on parent window | |
System.out.println("Parent Window Title: "+driver.getTitle()); | |
//Close all the windows (Parent as well as Child windows) | |
driver.quit(); | |
} | |
} |