新的窗口句柄在IE中消失 [英] New window handles disappearing in IE

查看:204
本文介绍了新的窗口句柄在IE中消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续的前一个问题的,我在这里问。

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

我以前也遇到过一个问题,用硒2.37.0在Internet Explorer 10切换窗口。我的C#程序将导航到一个页面,点击打开链接按钮,并尝试导航到新窗口中执行其他任务。我的程序不停地失败了,因此我决定,这是因为硒的窗口句柄。打开一个新窗口后,窗口句柄( driver.WindowHandles.Count )的数量增加1至2个,符合市场预期,但经过一定的时间,数量窗口句柄滴回落至1。

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.

我创建重现此问题的最少的样品网页。只需将下面的代码保存到 Test.aspx文件您的桌面上称为文件:

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>

下面是我的C#代码(在Visual Studio 2010中称为控制台项目 TestWindowSwitching打开此页面,点击该按钮,并打印一些输出):

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:

窗口的初始句柄数:1

Initial number of window handles: 1

窗口句柄数在59毫秒降到1

Number of window handles dropped to 1 in 59 ms

按Enter键,关闭...

Press Enter to close...

我以前创建的这个解决方法,所以这实际上是不是我的问题了。我只是得到的JavaScript命令(在这种情况下, window.open('Test.aspx文件')),并直接使用它来浏览网页,而无需打开一个新窗口。

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.

不过,正如我在以前的文章中提到,我觉得别人可能已经遇到过这个问题(的例如1 ,的例如2 ),因此,如果这其实是在硒的一个错误,这是值得研究的。或者,如果我做错了什么,建议是值得欢迎的。

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.

推荐答案

我相信我可能已经找到了解决方案。我走进IE浏览器 - > Internet选项 - >高级 - >重置Internet Explorer设置。这样做的,重启后,我不再有窗口切换的问题。我不知道为什么会这样,因为我几乎不使用IE浏览器;我无法想象,我改变任何重要设置。

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.

重置我的设置后,当我第一次打开IE浏览器,我回答了几个对话框。首先,我点击使用推荐的安全性和兼容性设置和OK。然后,在你想将Internet Explorer默认的浏览器?,我点击否和不要再问。最后,几个附加组件都可以使用了,我点击不启用。

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".

然后,当我试图运行一个程序,使用64位驱动程序。 (我假设我的IE是64位的,因为在开始菜单,程序下 C:\Program Files\ - 不是 C:\Program文件(x86)\ ,像我的一些其他程序),它会崩溃就行 IWebDriver司机=新InternetExplorerDriver(); 。错误如下:

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异常是未处理

InvalidOperationException was unhandled

意外错误启动Internet资源管理器。保护模式设置不是
同样为所有区域。启用保护模式必须设置为相同的值
(启用或禁用)的所有区域。 (NoSuchDriver)

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)

要解决这个问题,我走进了IE浏览器 - > Internet选项 - >安全,并检查启用保护模式的所有四个区。这个固定;当我跑我的程序,我不再有这个错误。这个问题(即IE浏览器驱动程序无法使用IE 10的默认设置工作)可能是一个问题值得文档化

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.

改变这些设置后,我的程序运行起来 - 但它这样做的极为缓慢的。只花了60秒,以六字符长的字符串输入到两个文本框。然后我切换到32位驱动程序(尽管有64位IE),并以正常速度运行。

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.

总结:如果您遇到的问题,IE浏览器司机下探窗口句柄,考虑重设你的IE设置。这似乎有固定的我。

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.

这篇关于新的窗口句柄在IE中消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆