新的窗口句柄在IE中消失,不能切换到新窗口 [英] New window handles disappearing in IE, can't switch to new window

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

问题描述

我使用与C#的最新版本硒(2.37.0)在Internet Explorer 10(采用最新的32位InternetExplorerDriver,2.37.0)登录到网页上,点击打开一个新的按钮。窗口,然后将焦点更改到新窗口

I am using the latest version of Selenium (2.37.0) with C# in Internet Explorer 10 (using the latest 32-bit InternetExplorerDriver, 2.37.0) to log in to a webpage, click on a button that opens a new window, and then changes focus to the new window.

本来我是用Firefox的这个代码,它的工作每次:

Originally I was using this code in Firefox, and it would work every time:

// Get handle for original window
string parentHandle = driver.CurrentWindowHandle;

// Click on button for new window
driver.FindElement(By.Id("buttonForNewWindow")).Click();

// Get list of all window handles
ReadOnlyCollection<string> allWindowHandles = driver.WindowHandles;

// Loop over all handles and switch to new child window
foreach (string handle in allWindowHandles)
{
    if (handle != parentHandle)
    {
        driver.SwitchTo().Window(handle);
    }
}



但是,这并不在Internet Explorer中运行。

But this doesn't work in Internet Explorer.

我已经查明的问题。之前,我打开新的窗口,我用 driver.WindowHandles.Count 来获取窗口句柄数,以及(如预期)它告诉我,只有一个窗口句柄。然后,当我点击按钮的新窗口,它告诉我(再次,如预期),有两个窗口句柄。但在此之前的代码获取到线 driver.SwitchTo()窗口(句柄); ,窗口句柄的数量已经下降回落到一个,均匀虽然这两个窗口仍然可见

I have pinpointed the problem. Before I open the new window, I use driver.WindowHandles.Count to get the number of window handles, and (as expected) it tells me that there is only one window handle. Then, when I click the button for the new window, it tells me (again, as expected) that there are two window handles. But before the code gets to the line for driver.SwitchTo().Window(handle);, the number of window handles has dropped back down to one, even though both windows are still visible.

所以窗口莫名其妙手柄之一是迷路。我已确认(使用大量 Console.WriteLine 语句),即是被丢弃的窗口句柄是新的。我还使用了秒表类花费的时间为新的窗口句柄被丢弃多久:

So somehow one of the window handles is getting lost. I have confirmed (using a bunch of Console.WriteLine statements) that the window handle that is dropped is the new one. I also used the Stopwatch class to time how long it takes for the new window handle to be dropped:

Stopwatch sw = new Stopwatch();

// Wait until the number of windows has changed from 1 to 2
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until((_driver) => { return _driver.WindowHandles.Count != windowsBefore; });

// Start stopwatch
sw.Start();

int numberOfWindows = driver.WindowHandles.Count;

while (numberOfWindows == 2)
{
    numberOfWindows = driver.WindowHandles.Count;
}

sw.Stop();

Console.Write("\nTime elapsed: " + sw.ElapsedMilliseconds + " ms");

这跑了几次后,我发现该窗口的句柄数通常降回一个< STRONG>在不到50毫秒。然而,一个时间窗口句柄持续更长的时间;这里是原来的数字:

After running this a few times, I found that the number of window handles typically drops back to one in less than 50 milliseconds. However, one time the window handle lasted much longer; here are the original numbers:

Time elapsed: 47 ms
Time elapsed: 31 ms
Time elapsed: 47 ms
Time elapsed: 2861 ms
Time elapsed: 30 ms

我还证实,无法在Firefox 会出现此问题。当我运行在Firefox相同的代码,我发现窗口的句柄数从1变为2的和保持这样的,因为它应该。

I have also confirmed that this problem does not occur in Firefox. When I run the same code in Firefox, I find that the number of window handles changes from 1 to 2, and stays that way, as it should.

似乎其他人有这个问题为好。 此人发现,当他们打开一个新窗口等待1000毫秒,getWindowHandles()返回1,而不是2的值,的这个人在硒谷歌集团似乎有同样的问题。

It seems that others are having this problem as well. This person finds that when they open a new window and wait 1000 milliseconds, getWindowHandles() returns a value of 1 instead of 2. And this person on the Selenium Google Group seems to be having the same issue.

我的问题是:鉴于办理一个新打开的窗口中保持打开(典型值)小于50毫秒,什么是硒切换到新的窗口,在Internet Explorer 10的最有效可行的方法?或者是有什么我应该做的不同,以防止新的窗口迷路处理?

My question: Given that the handle for a newly-opened window remains open for (typically) less than 50 milliseconds, what is the most efficient possible way to switch to a new window in Internet Explorer 10 with Selenium? Or is there something I should be doing differently to prevent new window handles from getting lost?

推荐答案

只是想给一个更新,如果任何人有这个同样的问题,发现这个网页。我还没有找到一个解决我的问题,但我想出了一个解决方法。

Just wanted to give an update, in case anyone else has this same problem and finds this page. I haven't found a solution to my problem, but I came up with a workaround.

本来我是点击该打开了一个新窗口中的链接,我遇到了麻烦硒导航到新窗口。我所做的却是通过页面的源代码挖掘,发现我点击按钮,并找出其中的按钮点。这本来是最简单的,如果它是一个简单的超链接(www.whatever.com / ......),而是它是一个JavaScript函数。这JavaScript函数建立了使用一些固定的字符和某些变量的超级链接;例如:

Originally, I was clicking on a link that opened a new window, and I was having trouble navigating Selenium to the new window. What I did instead was dig through the page's source code, find the button that I was clicking on, and figure out where that button points to. It would have been easiest if it was a simple hyperlink (www.whatever.com/....), but instead it was a Javascript function. That Javascript function builds up the hyperlink using some fixed characters and some variables; for example:

link(username, date) = "http://www.google.com/user=" + username + "?date=" + date;



所以,用这个例子一起去,我只是想出什么变量中(用户名被喂食,日期等),建立了自己的网址,并直接导航到它。因此,我甚至没有打开并导航到一个新的窗口都没有。

So, going along with this example, I just figured out what variables were being fed in (username, date, etc.), built the URL myself, and navigated to it directly. Thus I didn't even have to open up and navigate to a new window at all.

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

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