How to switch to a new browser window that opens after clicking a button?

I have a situation, when I click the button, a new browser window opens with the search results.

Can I connect and focus on the new browser window that opens?

And work with it, then return to the original (first) window.

+61
java selenium selenium-webdriver webdriver
Mar 06 2018-12-12T00:
source share
9 answers

You can switch between windows as shown below:

// Store the current window handle String winHandleBefore = driver.getWindowHandle(); // Perform the click operation that opens new window // Switch to new window opened for(String winHandle : driver.getWindowHandles()){ driver.switchTo().window(winHandle); } // Perform the actions on new window // Close the new window, if that window no more required driver.close(); // Switch back to original browser (first window) driver.switchTo().window(winHandleBefore); // Continue with original browser (first window) 
+85
Mar 07 '12 at 7:59
source share

Just add to the content ...

Return to the main window (default window).

use driver.switchTo().defaultContent();

+11
Jan 22 '13 at 9:03
source share

This script allows you to switch from the Parent window to the child window and return cntrl to the parent window

 String parentWindow = driver.getWindowHandle(); Set<String> handles = driver.getWindowHandles(); for(String windowHandle : handles) { if(!windowHandle.equals(parentWindow)) { driver.switchTo().window(windowHandle); <!--Perform your operation here for new window--> driver.close(); //closing child window driver.switchTo().window(parentWindow); //cntrl to parent window } } 
+7
Jun 24 '13 at 12:26
source share

I use an iterator and while loop to store various window handles, and then switch back and forth.

 //Click your link driver.findElement(By.xpath("xpath")).click(); //Get all the window handles in a set Set <String> handles =driver.getWindowHandles(); Iterator<String> it = handles.iterator(); //iterate through your windows while (it.hasNext()){ String parent = it.next(); String newwin = it.next(); driver.switchTo().window(newwin); //perform actions on new window driver.close(); driver.switchTo().window(parent); } 
+2
Jun 01 2018-12-12T00:
source share

Surya, your path will not work for two reasons:

  • You cannot close the driver while evaluating the test, as it will lose focus before switching to the active element and you will get a NoSuchWindowException.
  • If the test is running on ChromeDriver, you will not get a window, but a tab on a click in your application. Since SeleniumDriver cannot act with tabs , it only switches between windows, it freezes when you click where a new tab opens and a timeout fails.
+2
Jul 31 '12 at 10:20
source share

You can use:

 driver.SwitchTo().Window(WindowName); 

Where WindowName is a string representing the name of the window you want to switch focus to. Call this function again with the name of the original window to return to it when you are done.

+1
Mar 06 '12 at 19:38
source share

Change registry for IE:

ie_x64_tabprocgrowth.png

  • HKEY_CURRENT_USER \ Software \ Microsoft \ Internet Explorer \ Main
  • Right click -> New -> String Value -> Value Name: TabProcGrowth (create if doesn't exist)
  • TabProcGrowth (right-click) โ†’ Change ... โ†’ Value data: 0

Source: Problem switching Windows Selenium WebDriver in Internet Explorer 8-10

In my case, IE began to detect new window handles after editing the registry.

Adapted from the MSDN blog:

Tab Process Growth: Sets the speed at which IE creates new Tab processes.

Max-Number Algorithm: Specifies the maximum number of tab processes that can be performed for a single isolation session for a single-frame process at a specific mandatory integrity level (MIC). Relative values:

  • TabProcGrowth = 0: tabs and frames are executed within the same process; frames are not unified by MIC levels.
  • TabProcGrowth = 1: all tabs for a given frame process are executed in one tab process for a given MIC level.

Source: Opening a new tab may launch a new process using Internet Explorer 8.0




Internet Options:

  • Security โ†’ Deny Enable secure mode for all zones (Internet, Local intranet, Trusted sites, Restricted sites)
  • Advanced โ†’ Security โ†’ Disable Enable Enhanced Protected Mode



the code:

Browser: IE11 x64 (Zoom: 100%)
OS: Windows 7 x64
Selenium: 3.5.1
WebDriver: IEDriverServer x64 3.5.1

 public static String openAndSwitchToWindow(WebDriver driver, WebElement clickableElement, long waitInMillis) throws InterruptedException { String parentHandle = driver.getWindowHandle(); // Save parent window clickableElement.click(); // Open child window Thread.sleep(waitInMillis); Set<String> handles = driver.getWindowHandles(); if (handles.size() > 1) { // Check if more than 1 window is open for (String handle : handles) { driver.switchTo().window(handle); if (!parentHandle.equals(handle)) { break; } } } return parentHandle; // Returns parent window if you want to switch back } /* How to use method */ String parentHandle = SeleniumUtil.openAndSwitchToWindow(driver, linkToChildWindow, 5000); // Do things in child window driver.manage().window().maximize(); driver.close(); // Return to parent window driver.switchTo().window(parentHandle); 

The above code contains an if check to make sure you are not switching to the parent window since Set<T> does not have a guaranteed order in Java . Thread.sleep() seems to increase the likelihood of success, as confirmed below.

Quote from Luc Inman-Semerau : (Developer for Selenium)

It will take time for the browser to confirm the new window, and you can go into the switchTo () loop until the popup appears.

You automatically assume that the last window returned by getWindowHandles () will be the last. This is not necessarily true, as they are not guaranteed to be returned in any order.

Source: Unable to handle popup in IE, control does not migrate to popup




Related posts:

  • Switch selenium to new IE tab
  • How to open a new tab in IE using selenium (java) and open the url to this tab (not a window)
+1
Aug 13 '17 at 14:07 on
source share

If you have more than one browser (using java 8)

 import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class TestLink { private static Set<String> windows; public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("file:///C:/Users/radler/Desktop/myLink.html"); setWindows(driver); driver.findElement(By.xpath("//body/a")).click(); // get new window String newWindow = getWindow(driver); driver.switchTo().window(newWindow); // Perform the actions on new window String text = driver.findElement(By.cssSelector(".active")).getText(); System.out.println(text); driver.close(); // Switch back driver.switchTo().window(windows.iterator().next()); driver.findElement(By.xpath("//body/a")).click(); } private static void setWindows(WebDriver driver) { windows = new HashSet<String>(); driver.getWindowHandles().stream().forEach(n -> windows.add(n)); } private static String getWindow(WebDriver driver) { List<String> newWindow = driver.getWindowHandles().stream() .filter(n -> windows.contains(n) == false).collect(Collectors.toList()); System.out.println(newWindow.get(0)); return newWindow.get(0); } } 
-one
Oct. 06 '14 at 12:08
source share

I like the way to automatically switch windows, which I could control using page objects.

When multiple windows open, any method from this page object will be executed in a browser window that has a corresponding title.

 @Window(".* some windows title goes here .*") public class ToolsQANewWindow { // your page object code } 

Check out the detailed steps here - http://www.testautomationguru.com/selenium-webdriver-automatic-switching-between-browser-windows-using-guice-method-interceptor/

-2
Jun 06 '17 at 10:20
source share



All Articles