c# - 新窗口句柄在 IE 中消失

标签 c# internet-explorer selenium

这是 a previous question 的后续行动我在这里问过。

我之前遇到过使用 Selenium 2.37.0 在 Internet Explorer 10 中切换窗口的问题。我的 C# 程序将导航到一个页面,单击一个打开链接的按钮,然后尝试导航到新窗口以执行其他任务。我的程序一直失败,我确定这是因为 Selenium 的窗口句柄。打开一个新窗口后,窗口句柄的数量 (driver.WindowHandles.Count) 正如预期的那样从 1 增加到 2,但经过一段时间后,窗口句柄的数量回落到1.

我创建了一个重现此问题的最小示例网页。只需将以下代码保存到桌面上名为 test.aspx 的文件中:

<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 的控制台项目)打开此页面、单击按钮并打印一些输出:

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();
        }
    }
}

这是该代码的一些示例输出:

Initial number of window handles: 1

Number of window handles dropped to 1 in 59 ms

Press Enter to close...

我之前为此创建了一个解决方法,所以这对我来说实际上不再是问题了。我只需获取 Javascript 命令(在本例中为 window.open('test.aspx'))并直接使用它导航到页面,而无需打开新窗口。

但是,正如我在之前的帖子中提到的,我觉得其他人可能遇到过这个问题(example 1example 2),所以如果这实际上是 Selenium 中的错误,则值得调查。或者,如果我做错了什么,欢迎提出建议。

最佳答案

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

在这个过程中,我注意到了一些可能相关的问题。为了绝对透明,我会陈述我所做的一切。

重置我的设置后,当我第一次打开 IE 时,我回答了几个对话框。首先,我单击“使用推荐的安全性和兼容性设置”和“确定”。然后,在“您想将 Internet Explorer 设为默认浏览器吗?”下,我单击“否”和“不再询问”。最后,对于“Several add-ons are ready for use”,我点击了“Don't enable”。

然后,当我尝试使用 64 位驱动程序运行程序时。 (我假设我的 IE 是 64 位的,因为在“开始”菜单中,程序位于 C:\Program Files\ 下——而不是 C:\Program Files (x86)\,就像我的其他一些程序一样。)它会在 IWebDriver driver = new InternetExplorerDriver(); 行崩溃。错误如下:

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)

为了解决这个问题,我进入 IE -> Internet 选项 -> 安全并为所有四个区域选中“启用保护模式”。这修复了它;当我运行我的程序时,我不再遇到那个错误。这个问题(IE 驱动程序无法使用 IE 10 的默认设置)可能是一个值得记录的问题。

更改这些设置后,我的程序运行了——但运行速度非常慢。将六个字符长的字符串输入两个文本框实际上需要 60 秒。然后我切换到 32 位驱动程序(尽管有 64 位 IE),它以正常速度运行。

最后,我一直运行我的程序,它不再丢弃窗口句柄。

总结:如果您遇到 IE 驱动程序丢弃窗口句柄的问题,请考虑重置您的 IE 设置。这似乎已为我解决了这个问题。

关于c# - 新窗口句柄在 IE 中消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19777318/

相关文章:

c# - 使用输入参数调用 SSIS 包中的存储过程

c# - 整数溢出 - 将 C# 转换为 VB.Net 代码

html - 最小宽度在 IE 中不适用于卡住布局

html - 这个href打开新窗口在ie7中不起作用?

javascript - Selenium:滚动到页面末尾

extjs - 在 Selenium Webdriver 中使用 Element 的工具提示作为定位器

python - 在 Selenium [Python] 中使用 PhantomJS 进行透明截图?

c# - 为什么即使创建了显式运算符我也无法将源类型转换为字符串?

c# - bool 转换问题的枚举

css - IE在使用css渐变的div中添加蓝色边框