Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

c# - New window handles disappearing in IE

This is a follow-up to a previous question that I asked here.

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:

<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
    <input id="btn" type="button" value="Link"  lang="javascript"     onclick="window.open('test.aspx')">
</body>
</html>

Here is my C# code (a Console project in Visual Studio 2010 called TestWindowSwitching) that opens this page, clicks the button, and prints some output:

using System;
using System.Threading;         // Needed for Sleep
using System.Diagnostics;       // Needed for Stopwatch

using OpenQA.Selenium;
using OpenQA.Selenium.IE;

namespace TestWindowSwitching
{
    class Program
    {
        static void Main()
        {
            IWebDriver driver = new InternetExplorerDriver();

            driver.Navigate().GoToUrl("C:\Users\yourNameHere\Desktop\test.aspx");

            try { driver.SwitchTo().DefaultContent(); }
            catch { Thread.Sleep(10); }

            Console.WriteLine("Initial number of window handles: " + driver.WindowHandles.Count);

            Stopwatch sw = new Stopwatch();

            try { driver.FindElement(By.Id("btn")).Click(); }
            catch { Thread.Sleep(10); }

            // Wait until number of window handles becomes 2
            while (driver.WindowHandles.Count != 2)
            {
                // Do nothing
            }

            sw.Start();

            // Wait until number of window handles changes from 2
            while (driver.WindowHandles.Count == 2)
            {
                // Do nothing
            }

            Console.WriteLine("Number of window handles has dropped to " + driver.WindowHandles.Count
                                   + " in " + sw.ElapsedMilliseconds + " ms");

            sw.Stop();

            Console.Write("Press Enter to close...");
            Console.ReadLine();

            driver.Quit();
        }
    }
}

Here's some sample output for that code:

Initial number of window handles: 1

Number of window handles dropped to 1 in 59 ms

Press Enter to close...

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...