7

My application opens up a new window on clicking a button and i need to perform some actions in that window. But the response getWindowHandles() method of selenium webdriver has only one window id in it. This happens especially if there is a delay in calling the getWindowHandles() after opening the new window. There is a known issue with selenium. https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration

But even the solution for that is not working for me.

Code is as follows

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
RemoteWebDriver driver = new
        RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);

driver.get("https://<url>");

WebElement userName = driver.findElement(By.name("usr_name"));
userName.sendKeys("ABCD");

WebElement password = driver.findElement(By.name("usr_password"));
password.sendKeys("password");

WebElement login = driver.findElement(By.name("OK"));
login.click();  


WebElement popup= driver.findElement(By.name("popup"));
popup.click();      

Thread.sleep(1000);

Set<String> windowHandles = driver.getWindowHandles();      
System.out.println(windowHandles);

The Set "windowHandles" will return only one window :

"[fcdad457-9090-4dfd-8da1-acb9d6f73f74]" 

But if i remove the sleep. it will return two window ids :

[90cc6006-0679-450c-a5b3-6602bcb41a16, 7211bbfd-2616-4460-97e7-56c0e632c3bb]

I cannot remove the sleep as this is just a sample program and in the real application there will be some delay in between. Please let me know your thoughts.This issue is only for IE11.

Blue screen - Home Page; Grey Screen - Popup

enter image description here

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
Renjith
  • 960
  • 1
  • 16
  • 39
  • Could you give a whole screen shot of your pop up? I want to indentify it's a actual new browser window/Tab or simulated pop-up – yong Jan 09 '18 at 08:22
  • @yong Added the screenshot – Renjith Jan 09 '18 at 09:17
  • Which one is the pop-up you mean? The big window with gray background? or the small one with white background the most front? – yong Jan 09 '18 at 09:44
  • The big window with grey background. What you see behind that (blue background), is the parent window. – Renjith Jan 09 '18 at 09:50
  • Try the solution in this post. If not fix your issue, then try use latest IEDriver – yong Jan 09 '18 at 12:08
  • did you mean to set the protected mode for all zones? . Unfortunately that is not possible. i'll try with new driver. – Renjith Jan 09 '18 at 12:28
  • Sorry, I forgot add the post link: https://github.com/SeleniumHQ/selenium/issues/2028 – yong Jan 09 '18 at 12:38
  • :(. not working – Renjith Jan 09 '18 at 12:54
  • HI @Renjith: Did you get solution for your issue? I am using Selenium with IE 11. When I click on a button, the original window gets closed and another window is opened using window.open in javascript. The code is losing window handles. I have unchecked "Enable protected mode" for all zones in IE. driver.windowhandles.count returns 0 always. Any help will be a real life safer. – harsha.cs May 26 '21 at 16:15
  • @harsha.cs unfortunately I did not – Renjith May 27 '21 at 08:17

4 Answers4

6

There a couple of things which you have to take care while dealing with InternetExplorer as follows :

As you mentioned There is a known issue with selenium documented in GitHub, these are not issues as such but is the combined set of Required Configuration while dealing with InternetExplorer. Without taking care of these settings InternetExplorer may not behave as per expectation. The following items are critical to demonstrate proper behavior of InternetExplorer v11 :

  • Enhanced Protected Mode must be disabled for IE 10 and higher. This option is found in the Advanced tab of the Internet Options dialog.
  • The browser Zoom Level must be set to 100% so that the native mouse events can be set to the correct coordinates.
  • You have to set Change the size of text, apps, and other items to 100% in display settings.
  • For IE 11, you will need to set a registry entry on the target computer so that the driver can maintain a connection to the instance of Internet Explorer it creates.

    For 32-bit Windows installations, the key you have to look in the registry is : 
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE
    
    For 64-bit Windows installations, the key is :
    HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE
    
    The FEATURE_BFCACHE subkey may or may not be present, and should be created if it is not present.
    
  • Native Events : The advantage of using native events is that it does not rely on the JavaScript sandbox, and it ensures proper JavaScript event propagation within the browser. However, there are currently some issues with mouse events when the IE browser window does not have focus, and when attempting to hover over elements.

  • Browser Focus : IE itself appears to not fully respect the Windows messages we send the IE browser window (WM_MOUSEDOWN and WM_MOUSEUP) if the window doesn't have the focus.

  • You can find a detailed discussion on Native Events and Browser Focus here.

  • Now, you have to configure all these parameters through DesiredCapabilities Class as follows :

    DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
    cap.setCapability("ignoreProtectedModeSettings",1);
    cap.setCapability("IntroduceInstabilityByIgnoringProtectedModeSettings",true);
    cap.setCapability("nativeEvents",true);
    cap.setCapability("browserFocus",true);
    cap.setCapability("ignoreZoomSetting", true);
    cap.setCapability("requireWindowFocus","true");
    cap.setCapability("INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS", true);
    
  • As per Best Programming practices Thread.sleep(1000); is a huge No as it degrades the Test Performance

  • Now, as you are aware that the Browser Clients lags the WebDriver instance so we have to often sync up them. So before you collect the windowHandles you have to induce WebDriverWait as follows for which you can find a detailed discussion here :

    WebElement popup= driver.findElement(By.name("popup"));
    popup.click();
    new WebDriverWait(driver,5).until(ExpectedConditions.numberOfWindowsToBe(2));
    Set<String> windowHandles = driver.getWindowHandles();      
    System.out.println(windowHandles);
    

Update

I can see from your comments :

"Enable Enhanced Protected Mode" is unchecked in IE options. – Renjith Jan 9 at 7:26

Here is the exert from @JimEvans sensetional blog on Protected Mode settings and the Capabilities hack where @JimEvans nails the context in a clear and unambiguous term :

When the rewritten IE driver was first introduced, it was decided that it would enforce its required Protected Mode settings, and throw an exception if they were not properly set. Protected Mode settings, like almost all other settings of IE, are stored in the Windows registry, and are checked when the browser is instantiated. However, some misguided IT departments make it impossible for developers and testers to set even the most basic settings on their machines.

The driver needed a workaround for people who couldn't set those IE settings because their machine was overly locked down. That's what the capability setting is intended to be used for. It simply bypasses the registry check. Using the capability doesn't solve the underlying problem though. If a Protected Mode boundary is crossed, very unexpected behavior including hangs, element location not working, and clicks not being propagated, could result. To help warn people of this potential problem, the capability was given big scary-sounding names like INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS in Java and IntroduceInstabilityByIgnoringProtectedModeSettings in .NET. We really thought that telling the user that using this setting would introduce potential badness in their code would discourage its use, but it turned out not to be so.

If you are able to set the Protected Mode settings of IE, and you are still using the capability you are risking the stability of your code. Don't do it. Set the settings. It's not that hard.

Here is how you need to set the Protected Mode settings :

Protected Mode IE

Moshe Slavin
  • 4,696
  • 5
  • 18
  • 34
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • I tried all these, but dint worked. I agree that 'Thread.sleep()' is not a good practice. I was trying to simulate the delay in actual code using sleep(). (what i have given in the question is a sample code for testing, not the actual code in production). I have even removed the 'sleep()' and kept a scanner in between, so that rest of the code works only on receiving my input. It looks like the webdriver is loosing the second window when there a delay in accessing the second window. – Renjith Jan 16 '18 at 14:24
  • "Enable Enhanced Protected Mode" is in the advanced tab. I talked to IT department about setting the 'Enable Protected Mode', but they said it is difficult. – Renjith Jan 19 '18 at 12:54
  • ***Is the desiredCapabilites retained in the new winodow that opens?*** or some additional configuration is needed for that. – Deepak Yadav Dec 17 '19 at 10:22
4

Window handling issue, is mainly because of protected mode settings. Either enable protected mode for all the zones or disable it for all the zone and try.

ArunPrakash
  • 128
  • 1
  • 7
  • "Enable Enhanced Protected Mode" is unchecked in IE options. – Renjith Jan 09 '18 at 07:26
  • I am not able to enable it for local intranet and Trusted sites zones. It is a company provided laptop and these are set based on user groups. Any other solutions? – Renjith Jan 09 '18 at 12:06
  • No other way. Its all have to be same. Raise an exception at your organization and get it approved. This is a required configuration and not an available configuration. – ArunPrakash Jan 10 '18 at 06:47
  • Switch your testing to Google Chrome. I avoid IE like the plague. – MikeJRamsey56 Jan 16 '18 at 17:36
2

Dunno what is Set, but I tested with following code

while (true)
            {
                int qw = ololo.WindowHandles.Count;
                string[] wh = ololo.WindowHandles.ToArray();
                ololo.FindElement(By.LinkText("Помощь")).Click();
                Thread.Sleep(1000);
            }

And it worked perfectly.

Somber
  • 403
  • 3
  • 10
0

On IE11, "Enable Protected Mode" setting on the browser is key - can be either ON or OFF (for all zones).

Other settings on driver capabilities didn't matter (in my case) - following worked just as fine:

  caps.setCapability("ignoreZoomSetting", false);
  caps.setCapability("nativeEvents", false); 
  caps.setCapability("ignoreProtectedModeSettings", false);
Ravi
  • 1
  • 1