Source: Selenium WebDriver windows switching issue in Internet Explorer 8-10
For my case, IE began detecting new window handles after the registry edit.
Tab Process Growth : Sets the rate at which IE creates New Tab processes.
The "Max-Number" algorithm: This specifies the maximum number of tab processes that may be executed for a single isolation session for a single frame process at a specific mandatory integrity level (MIC). Relative values are:
- TabProcGrowth=0 : tabs and frames run within the same process; frames are not unified across MIC levels.
- TabProcGrowth =1: all tabs for a given frame process run in a single tab process for a given MIC level.
Source: Opening a New Tab may launch a New Process with Internet Explorer 8.0
Browser: IE11 x64 (Zoom: 100%)
OS: Windows 7 x64
Selenium: 3.5.1
WebDriver: IEDriverServer x64 3.5.1
public static String openWindow(WebDriver driver, By by) throws IOException {
String parentHandle = driver.getWindowHandle(); // Save parent window
WebElement clickableElement = driver.findElement(by);
clickableElement.click(); // Open child window
WebDriverWait wait = new WebDriverWait(driver, 10); // Timeout in 10s
boolean isChildWindowOpen = wait.until(ExpectedConditions.numberOfWindowsToBe(2));
if (isChildWindowOpen) {
Set<String> handles = driver.getWindowHandles();
// Switch to child window
for (String handle : handles) {
driver.switchTo().window(handle);
if (!parentHandle.equals(handle)) {
break;
}
}
driver.manage().window().maximize();
}
return parentHandle; // Returns parent window if need to switch back
}
/* How to use method */
String parentHandle = Selenium.openWindow(driver, by);
// Do things in child window
driver.close();
// Return to parent window
driver.switchTo().window(parentHandle);
The above code includes an if-check to make sure you are not switching to the parent window as Set<T>
has no guaranteed ordering in Java. WebDriverWait
appears to increase the chance of success as supported by below statement.
The browser may take time to acknowledge the new window, and you may be falling into your switchTo() loop before the popup window appears.
You automatically assume that the last window returned by getWindowHandles() will be the last one opened. That's not necessarily true, as they are not guaranteed to be returned in any order.
Source: Unable to handle a popup in IE,control is not transferring to popup window