Can selenium handle Windows based pop up?


Selenium can handle Windows based pop up. There may be scenarios where a web page opens more than one window after performing some actions on it. The child windows that get opened may be a pop up containing some information or advertisement.

Selenium uses the getWindowHandles () and getWindowHandle () methods to work with child windows. The getWindowHandles () method contains all the window handle ids of the opened windows. The window id handles are held in the form of Set data structure [containing data type as String].

The getWindowHandle () method is used to store the window handle id of the present active window. As we know, getWindowHandles () method is used to store all the opened window handle ids. To iterate through all the handles, iterator () and next () methods are used.

Since getWindowHandles () stores the window ids in the form of Set data structure, we have to import java.util.Set in our code. Also, for using the iterator () method, we have to import java.util.Iterator and import java.util.List.

Finally to switch to a particular window, switchTo.().window() method is used. The handle id of the window where we want to switch is passed as an argument to that method.

The steps to be followed to implement the above concept −

  • After the application is launched, let us first store all the window handle ids in a Set data structure with the help of getWindowHandles () method.

  • We shall iterate through all the window handle ids with the help of iterator () and next () methods.

  • Let us then grab the current window handle id with the help of getWindowHandle () method.

  • Then switch to that window with switchTo.().window() method.

Example

Code Implementation.

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;
import java.util.List;
import java.util.Set;
import java.util.Iterator;
public class HandleWindows {
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://secure.indeed.com/account/login";
      driver.get(url);
      //implicit wait
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
      // store all window handles
      Set<String> a = driver.getWindowHandles();
      // iterate through handles
      Iterator<String> it = a.iterator();
      String chlwnd = it.next();
      String pwnd = it.next();
      // switch to child window
      driver.switchTo().window(chlwnd);
      System.out.println("Page title "+ driver.getTitle());
      // switch to parent window
      driver.switchTo().window(pwnd);
      System.out.println("Page title "+ driver.getTitle());
      driver.quit();

Updated on: 28-Aug-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements