Multiple Browser windows

Some Web application may have multiple windows & many frames. During Multiple window's navigation Your operating system assigns an alphanumeric id to each window as soon as the Browser object is instantiated.

This id is called window's GuID, In simple terms, each unique window/tab has a unique ID, so that Selenium kotlin can differentiate when it is switching controls from one window to the other.

Kotlin selenium uses the GuId of the window or tab to for moving control from one window to another windows or tab

You get the current windows GUId using below command.


							//Returns a string of unique alphanumeric window handle
    val handle = driver.windowHandle
							

You can get the all the unique GuId of the opened windows, this will return a set of Browser window GuIDs


							//Returns a set of window handle
    val allHandles = driver.windowHandles
							

As I said earlier selenium Kotlin support moving the control between the different windows using switchTO() function present in selenium


							driver.switchTo().window(window_GuId);
							

Handle simple Two Browser Windows / Tabs

Click the button to Open a new window ( google.com)

Please use firefox as browser to open the link in new window, if you use chrome the link may be opened in new tab rather than new window.

two-windows-example-selenium-webdriver Steps to Handle Two Windows :
1. Open firefox browser and Navigate to https://chercher.tech/kotlin-selenium/windows


										val driver = new FirefoxDriver();
										// set implicit time to 30 seconds
										driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
										// navigate to the url
										driver.get("https://chercher.tech/kotlin-selenium/windows");
										


2. Get the GU ID of the current (parent) window using windowHandle method present in the selenium and store the value in a String


										// get the Session id of the Parent
										val parentGUID = driver.windowHandle;
										


3. Click on the Open New Window button, application open new window with google page. Make the selenium webdriver to sleep for 5 seconds, otherwise it may not find the newly opened tab.


										// click the button to open new window
										driver.findElement(By.id("two-window")).click();
										Thread.sleep(5000);
										


4. Get the GU IDs of the two windows (parent + google), using windowHandles method present in the Selenium. Store the GU IDs in a Set Collection, this Set will have GU IDs of both parent and Child Browsers


										// get the All the session id of the browsers
										val allGUID = driver.windowHandles;
										


5. iterate the Set of GUID values, and if the value is parent value skip it if not switch to the new window


										// iterate the values in the set
										for(String guid : allGUID){
											// one enter into if blobk if the GUID is not equal to parent window's GUID
											if(! guid.equals(parentGUID)){
												// switch to the guid
												driver.switchTo().window(guid);
												// break the loop
												break;
											}
										}
										


6. Switch to window using switchTo().window() method present in selenium, pass the GU ID of the child browser to this method.


										// switch to the guid
										driver.switchTo().window(guid);
												
										


7. Find the search bar in Google.com and search for "success"


										// search on the google page
										driver.findElement(By.name("q")).sendKeys("success");
										


Close the Google tab/Window and return to parent tab/browser window


										// close the browser
										driver.close();
										// switch back to the parent window
										driver.switchTo().window(parentGUID);
										

Complete code for switching window may look like below


										import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.By
import java.util.concurrent.TimeUnit

fun main(args: Array) {
    // open firefox browser
    System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe")
    val driver = FirefoxDriver()
    // set implicit time to 30 seconds
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS)
    // navigate to the url
    driver.get("https://chercher.tech/kotlin-selenium/windows")
    // get the Session id of the Parent
    val parentGUID = driver.windowHandle
    // click the button to open new window
    driver.findElement(By.id("two-window")).click()
    Thread.sleep(5000)
    // get the All the session id of the browsers
    val allGUID = driver.windowHandles

    // pint the title of th epage
    println("Page title before Switching : " + driver.title)
    println("Total Windows : " + allGUID.size)
    // iterate the values in the set
    for (guid in allGUID) {
        // one enter into if blobk if the GUID is not equal to parent window's GUID
        if (guid != parentGUID) {
            // switch to the guid
            driver.switchTo().window(guid)
            // break the loop
            break
        }
    }
    // search on the google page
    driver.findElement(By.name("q")).sendKeys("success")
    // print the title after switching
    println("Page title after Switching to goolge : " + driver.title)
    Thread.sleep(5000)
    // close the browser
    driver.close()
    // switch back to the parent window
    driver.switchTo().window(parentGUID)
    // print the title
    println("Page title after switching back to Parent: " + driver.title)
}

										


output-two-window-selenium-webdriver

More than Two Window handling

We have handled the two windows in above tutorial by comparing GUID but when we have more than two window we cannot use the same approach. To access the exact page we may need title of the page, based on the title of the page we can differentiate the browser windows in selenium.

Tip : We can use title, url, element reference while switching into new window to ensure that we are switching to the required window. I prefer Tile, url than element



Scenario : Click on the Open 3 New Windows button, it will open three more windows (bing, google, yahoo), now switch to bing and search for "gates". four-windows-example-selenium-webdriver Steps to Handle More than Two Windows :
1. Open firefox browser and Navigate to https://chercher.tech/kotlin-selenium/windows

2. Get the GU ID of the current (parent) window using windowHandle method present in the selenium kotlin and store the value in a String

3. Click on the Open 3 New Window button, application open new window with google page. Make the selenium kotlin to sleep for 5 seconds, otherwise it may not find the newly opened tab.

4. Get the GU IDs of the two windows (parent + google + bing + yahoo), using windowHandles method present in the Selenium kotlin. Store the GU IDs in a Set Collection, this Set will have GU IDs of both parent and Child Browsers

5. Now iterate through the Set,switch to first GUID in Set and check the page title contains the keyword "bing".

Example program for switching multiple windows in Kotlin selenium


										import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.By
import java.util.concurrent.TimeUnit

fun main(args: Array) {
    // open firefox browser
    System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe")
    val driver = FirefoxDriver()
    // set implicit time to 30 seconds
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS)
    // navigate to the url
    driver.get("https://chercher.tech/kotlin-selenium/windows")
    // get the Session id of the Parent
    val parentGUID = driver.windowHandle
    // click the button to open new window
    driver.findElement(By.id("three-window")).click()
    Thread.sleep(5000)
    // get the All the session id of the browsers
    val allGUID = driver.windowHandles
    // print number of windows
    println("Total Windows : " + allGUID.size)
    //iterate through windows
    for (guid in allGUID) {
        driver.switchTo().window(guid)
        // check the title of the page to match with "bing"
        if (driver.title.toLowerCase().contains("bing")) {
            driver.findElement(By.className("b_searchbox")).sendKeys("gates")
            Thread.sleep(3000)
            break
        }
    }
    // close all the windows
    driver.quit()
}

										

About Author

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

You can also share knowledge by Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions