I previously encountered an issue with switching windows in Internet Explorer 10 using Selenium 2.37.0. My C# program would navigate to a page, click a button that opens a link, and attempt to navigate to the new window to perform additional tasks. My program kept failing, and I determined that this was because of Selenium's window handles. After opening a new window, the number of window handles (driver.WindowHandles.Count) increases from 1 to 2, as expected, but after some amount of time, the number of window handles drops back down to 1.

I have created a minimal sample webpage that reproduces this issue. Simply save the following code into a file called test.aspx on your desktop:

I previously created a workaround for this, so this actually isn't a problem for me anymore. I simply get the Javascript command (in this case, window.open('test.aspx')) and use it directly to navigate to the page without opening a new window.

However, as I mentioned in my previous post, I feel that others may have encountered this issue (example 1, example 2), so if this is in fact a bug in Selenium, it is worth investigating. Or, if I am doing something wrong, suggestions are welcome.

The issue you're describing does not reproduce for me using your code in this question. Caveats: I'm hosting the file in a web server (which you should always do with the IE driver), not trying to open it from the hard drive using a file path. I'm also introducing a timeout, because the window handles never disappear, so I'm avoiding an infinite loop, but I've waited as long as 60 seconds for the window handle to disappear.
– JimEvansNov 5 '13 at 15:05

Thanks for trying it out, @JimEvans. Can you tell me more about why/how to host a file in a web server for the IE driver (or maybe direct me to somewhere that I can read more about it)? I suppose that could be a problem, but I originally encountered this issue on a webpage, rather than this page on my hard drive, so I don't think that's my issue here. It's strange that your window handles never disappear; my window handles typically drop down to 1 in 100 milliseconds or less. I'm using the latest versions of Selenium and the IE driver, so I'm puzzled why I'm having this issue and you aren't...
– Daniel CharlesNov 5 '13 at 15:12

Behavior when using a direct file path to open documents using the IE driver is undefined. It might work, it might not; it might work sporadically. This is because Protected Mode behavior for "file://" URLs is not (easily) settable, and may result in unintended Protected Mode boundary crossings. If you're experiencing problems with popup browser windows you must: 1. be using documents served from a web server, and 2. have Protected Mode settings properly set, not using the INTRODUCE_FLAKINESS... capability (or its equivalent). Anything other than this is not a bug in the IE driver.
– JimEvansNov 5 '13 at 15:17

Additionally, one other thing. If you're using Enhanced Protected Mode in IE (introduced, but disabled by default in IE10, enabled by default in IE11), all bets are off. Enhanced Protected Mode must be disabled for the IE driver to work.
– JimEvansNov 5 '13 at 15:19

@JimEvans Thanks for the info. I seem to have fixed my issue. I made a post (see below), but in short, resetting my IE settings seemed to do it. In the process, however, I noticed an issue: the IE driver doesn't seem to work with the default IE 10 settings. (Again, see my post below.) That may be an issue worth exploring.
– Daniel CharlesNov 5 '13 at 16:48

1 Answer
1

I believe I may have found the solution. I went into IE -> Internet options -> Advanced -> Reset Internet Explorer Settings. After doing this and rebooting, I no longer have the issue with window-switching. I'm not sure why this happened, because I hardly use IE; I can't imagine that I changed any important settings.

During this process, I noticed a few issues which may be relevant. To be absolutely transparent, I will state everything that I did.

After resetting my settings, when I first opened IE, I answered a few dialogs. First, I clicked "Use recommended security and compatibility settings" and "OK". Then, under "Would you like to make Internet Explorer your default browser?", I clicked "No" and "Don't ask again". Finally, for "Several add-ons are ready for use", I clicked "Don't enable".

Then, when I attempted to run a program, using the 64-bit driver. (I am assuming that my IE is 64-bit, because in the Start Menu, the program is under C:\Program Files\ -- not C:\Program Files (x86)\, like some of my other programs.) It would crash on the line IWebDriver driver = new InternetExplorerDriver();. The error was as follows:

InvalidOperationException was unhandled

Unexpected error launching Internet Explorer. Protected Mode settings are not
the same for all zones. Enable Protected Mode must be set to the same value
(enabled or disabled) for all zones. (NoSuchDriver)

To fix this, I went into IE -> Internet options -> Security and checked "Enable Protected Mode" for all four zones. This fixed it; when I ran my program, I no longer got that error. This problem (that the IE driver does not work with the default settings of IE 10) may be an issue worth documenting.

After changing those settings, my program ran -- but it did so extremely slowly. It literally took 60 seconds to type six-character-long strings into two text boxes. I then switched to the 32-bit driver (despite having 64-bit IE), and it runs at normal speed.

Finally, I ran my program all the way through, and it no longer drops window handles.

To summarize: If you're having issues with the IE driver dropping window handles, consider resetting your IE settings. That seems to have fixed it for me.

In point of fact, the need to properly configure the IE driver is well documented. The exception message is very explicit, the wiki page for the IE driver is very clear, and there are blog posts and conference talks that describe in excruciating detail why this is required.
– JimEvansNov 5 '13 at 18:04

Point taken. I've looked at that Google Code page before for a separate issue and must have missed the comments on Protected Mode. Any speculation as to why Selenium was dropping my new window handles before? This must been due to some faulty setting in IE (from before I reset the settings), but I'm inclined to believe that Protected Mode wasn't the issue: I never used that "FLAKINESS" option in my code, and I never got that exception before when creating an IE driver. Also, I originally encountered this issue on a webpage, so the potential issue with "file://" URLs wasn't the cause, either.
– Daniel CharlesNov 5 '13 at 18:54