Multiple Windows in Selenium

There will be times when you will require to perform some testing, where the testing operations open a new browser/tab; a test case may required you to perform some tasks on the newly opened browser window/tab and return back to the original window to perform the remaining tasks.

Even if the window/tab is currently on focus, but still, it is not an active window, so to perform some tasks, you need to switch to a new browser window/tab in webdriver.

Commands will have an effect on the base window unless we switch our control to a new window/tab.

Previously tab and Windows are two different items, as tabs don't have any GU iD. Now developers made tab and windows have GUIDs, so tabs and windows are treated as same in selenium.

Situations when we are likely to deal with multiple windows:

  • Filling forms may require selecting the date from a separately opened window
  • Clicking on some link/button can open another window
  • Handling Advertisement windows

Multiple Windows

multiple-windows-handling-selenium-webdriver

Above image depicts multiple browser windows

  • Our Application is opened on Parent/Base window, the total number of windows is one
  • When clicking on a link/button our application opens two new windows along with base window, now the total number of windows present is Three (base + child-1 + child-2)
  • Now move to Child-1 window and perform click on a button/link which opens another one more window along with child-window, now the total number of windows is 4 (base + child-1 + child-2 + grand child-1)
  • We can perform an operation on a grandchild -1 window, and we can close all windows or specific windows.

If we perform the above operation manually, we may not find difficulty, but handling the above scenario is tricky in selenium.

Get Window Handles in Selenium

Selenium provides few methods to handle the multiple windows; let's see what the methods and their uses are.

GU ID:
GU ID abbreviation of Globally Unique Identifier, Every OS generates GU ID for application to identifying them uniquely. We will be using this GU ID to handle the multiple browsers; GU ID is a numeric string value.

8589934593


getWindowHandle() :
getWindowHandle method in webdriver returns the current(active) browser's GU ID. It return GU ID as string value.

driver.getWindowHandle();


getWindowHandle() :
getWindowHandles method in selenium returns GU ID of all the browsers present at the moment, which are opened by the current driver. This method returns GU IDs as Set of String.

driver.getWindowHandles();


Set Collection in Java : Set is one of the Collection types, and it stores the values at random places in memory. We cannot access the values based on the index as Set stores values in random places ( this is one of the significant differences between List and Set). You may get wrong values if you try to access values based on the index.

switchTo().window() : switchTo() method in webdriver helps user to switch between windows, frames, elements, alerts. switchTo().window(GU ID) method switches the control from the current browser window to the target browser window, which has the specified "GU ID".

driver.switchTo().window(guid);

Create Custom By class Locator in Selenium

Handle simple Two Browser Windows / Tabs

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

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

two-windows-example-selenium-webdriver

Steps to Handle Two Windows :
1. Open Firefox browser and Navigate to https://chercher.tech/python/windows-selenium-python

WebDriver 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/python/windows-selenium-python.php");


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

// get the Session id of the Parent
String parentGUID = driver.getWindowHandle();


3. Click on the Open New Window button, application open new window with google page. Make the 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 the getWindowHandles() method present in the webdriver. 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
Set allGUID = driver.getWindowHandles();


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 block if the GUID is not equal to parent window's GUID
	if(! guid.equals(parentGUID)){
		//todo
	}
}


6. Switch to the window using switchTo().window() method, 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

public class TwoWindows {
	public static void main(String[] args) throws InterruptedException {
		// open firefox browser
		System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe");
		WebDriver 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/python/windows-selenium-python.php");
		// get the Session id of the Parent
		String parentGUID = driver.getWindowHandle();
		// 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
		Set<String> allGUID = driver.getWindowHandles();

		// print the title of the page
		System.out.println("Page title before Switching : "+ driver.getTitle());
		System.out.println("Total Windows : "+allGUID.size());
		// 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;
			}
		}
		// search on the google page
		driver.findElement(By.name("q")).sendKeys("success");
		// print the title after switching
		System.out.println("Page title after Switching to google : "+ driver.getTitle());
		Thread.sleep(5000);
		// close the browser
		driver.close();
		// switch back to the parent window
		driver.switchTo().window(parentGUID);
		// print the title
		System.out.println("Page title after switching back to Parent: "+ driver.getTitle());
	}
}

output-two-window-selenium-webdriver

Learn about NoSuchWindowException in selenium

More than Two Windows handling

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

Tip : We can use the title, URL, element reference while switching into a new window to ensure that we are switching to the required window. I prefer Tile, URL than the 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/python/windows-selenium-python

2. Get the GU ID of the current (parent) window using getWindowHandle() method present in the selenium 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 webdriver 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 getWindowHandles()method. 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 the first GUID in Set and check the page title contains the keyword "bing".

Complete program for switching multiple windows in selenium

public class MoreThanTwoWindows {
	public static void main(String[] args) throws InterruptedException {
		// open firefox browser
			System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe");
			WebDriver 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/python/windows-selenium-python.php");
			// get the Session id of the Parent
			String parentGUID = driver.getWindowHandle();
			// 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
			Set allGUID = driver.getWindowHandles();
			// print number of windows
			System.out.println("Total Windows : "+allGUID.size());
			//iterate through windows
			for(String guid : allGUID){
				driver.switchTo().window(guid);
				// check the title of the page to match with "bing"
				if(driver.getTitle().toLowerCase().contains("bing")){
					driver.findElement(By.className("b_searchbox")).sendKeys("gates");
					Thread.sleep(3000);
					break;
				}
			}
			// close all the windows
			driver.quit();
	}
}

Take Website Screenshot in selenium

How does the developer make the link to open in New Window

A developer can make the link to open in a new window using the target keyword present in anchor tag <a> when the developer sets this value to "_blank"; the link will be opened in new windows when the user clicks the link.

// button is nested inside a link
<a id='two-window' href='https://google.com' target='_blank'><input type='button' value="Open New Window"></a>
// plain link
<a id='two-window' href='https://google.com' target='_blank'>Click me</a>


If the developer avoids using target='_blank', the browser opens such links in the same window. It is recommended not to open the link in a new window.

<a id='two-window' href='https://google.com'>Click me</a>

Default Constructor in selenium and Java

Force to open in new Window rather than Tab

There will be situations where we have to open a link in a new window, but later browser does not support opening a URL in a new window as it is not recommended ( phishing is easy with new windows ).

During such scenarios, we can use action class to open the link in a new window by pressing the Shift key, and to click the link; it opens the link in a new window in the webdriver.

Steps to open link in a new Window :
1. Open Firefox browser and Navigate to https://chercher.tech/python/windows-selenium-python

// open firefox browser
System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe");
WebDriver driver = new FirefoxDriver();


2. Find the above link using id force-new-window and store it in a variable of WebElement type

// store the element
WebElement ele = driver.findElement(By.id("force-new-window"));


3. Create an object for Actions class

// create object for Actions class
Actions act = new Actions(driver);


4. Call the keyDown method from the Actions class object and pass Keys.SHIFT as parameter

5. Call Click method from the Actions class object, and pass the stored web element as a parameter to this method

6. Use build() method from Actions class object to bind the keyDown and click methods, call the perform method to perform the operations.

// press the shift key
act.keyDown(Keys.SHIFT)
//click the element
.click(ele)
// combine the actions
.build()
// perform the operations
.perform();


Complete program for opening a link in a new window in selenium.

public class OpenNewWindow {
	public static void main(String[] args) throws InterruptedException {
		// open firefox browser
		System.setProperty("webdriver.gecko.driver", "C:/Users/user/Pictures/geckodriver.exe");
		WebDriver 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/python/windows-selenium-python.php");
		// get the Session id of the Parent
		String parentGUID = driver.getWindowHandle();
		// store the element
		WebElement ele = driver.findElement(By.id("force-new-window"));
		// create object for Actions class
		Actions act = new Actions(driver);
		// press the shift key
		act.keyDown(Keys.SHIFT)
		//click the element
		.click(ele)
		// combine the actions
		.build()
		// perform the operations
		.perform();
	}
}

Handle dropdowns in selenium

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions