简体   繁体   English

Internet Explorer 8-10中的Selenium WebDriver Windows切换问题

[英]Selenium WebDriver windows switching issue in Internet Explorer 8-10

I found a problem trying to use Selenium WebDriver for testing our application. 我发现在尝试使用Selenium WebDriver测试我们的应用程序时遇到了问题。 The issue is in unstable pop-ups focusing in IE9. 问题在于IE9中不稳定的弹出窗口。 It is not always reproducible, it takes place in about 20% of windows switching but makes testing on IE almost impossible. 它并不总是可重现的,它发生在大约20%的窗口切换中,但几乎不可能在IE上进行测试。 In FireFox everything works perfect. 在FireFox中,一切都很完美。

  1. I try to increase timeout: 我尝试增加超时:

TimeSpan interval = new TimeSpan(0, 0, 10); driver.Manage().Timeouts().ImplicitlyWait(interval);

  1. Create own methods for objects finding: 为对象查找创建自己的方法:

      for (int x = 0; x <= waitTimeOut; x++) { try { element = (IWebElement)driver.FindElement(By.XPath(obj.Xpath)); return element; } catch{} } 
  2. Try to use CssSelecotrs 尝试使用CssSelecotrs

  3. Try to make some reswitching before finding element: 在找到元素之前尝试进行一些重新切换:

    driver.SwitchTo().Window(GetWindowHandle(2, 1)); driver.SwitchTo().Window(GetWindowHandle(0, 1)); driver.SwitchTo().Window(GetWindowHandle(2, 1));

If the issue occurs, it always occurs only with the first element I try to find on the page. 如果出现问题,它始终只发生在我尝试在页面上找到的第一个元素。 If the element is found there is no any problems with finding other elements on this page. 如果找到该元素,则在此页面上查找其他元素没有任何问题。 So I decided the problem is in focusing. 所以我认为问题在于聚焦。

Windows handles in debugger displays correctly. 调试器中的Windows句柄正确显示。 For example if I switches to the third window, driver.CurrentWindowHandle gives me correct handle of third window . 例如,如果我切换到第三个窗口,driver.CurrentWindowHandle给我第三个窗口的正确句柄。 But if I try to find any element, FindElement() throws me an exception. 但是如果我试图找到任何元素,FindElement()会抛出异常。 The page is loaded, I can click the element manually but FindElement() can't find it. 页面已加载,我可以手动单击该元素,但FindElement()无法找到它。 If I rerun the test, this step can be passed without any problems and failed only at the next switching or further. 如果我重新运行测试,则可以毫无问题地通过此步骤,并且仅在下一次切换或进一步切换时失败。 It's unpredictable. 这是不可预测的。

What can be the reason of such problem? 这样的问题可能是什么原因?

With the IE driver, the order in which the windows appear in the collection is not guaranteed. 使用IE驱动程序,无法保证窗口在集合中的显示顺序。 That is, the 0th window in the collection is not necessarily the first window opened by the session. 也就是说,集合中的第0个窗口不一定是会话打开的第一个窗口。 Given that is the case, you'll need to do something like the following: 鉴于这种情况,您需要执行以下操作:

private string FindNewWindowHandle(IWebDriver driver, IList<string> existingHandles, int timeout)
{
    string foundHandle = string.Empty;
    DateTime endTime = DateTime.Now.Add(TimeSpan.FromSeconds(timeout));
    while (string.IsNullOrEmpty(foundHandle) && DateTime.Now < endTime)
    {
        IList<string> currentHandles = driver.WindowHandles;
        if (currentHandles.Count != existingHandles.Count)
        {
            foreach (string currentHandle in currentHandles)
            {
                if (!existingHandles.Contains(currentHandle))
                {
                    foundHandle = currentHandle;
                    break;
                }
            }
        }

        if (string.IsNullOrEmpty(foundHandle))
        {
            System.Threading.Thread.Sleep(250);
        }
     }

     // Note: could optionally check for handle found here and throw
     // an exception if no window was found.
     return foundHandle;
}

The usage of the above function would be something like the following: 上述功能的用法如下:

IList<string> handles = driver.WindowHandles;
// do whatever you have to do to invoke the popup
element.Click();
string popupHandle = FindNewWindowHandle(driver, handles, 10);
if (!string.IsNullOrEmpty(popupHandle))
{
    driver.SwitchTo().Window(popupHandle);
}

如果是IE11,请修改HKLM_CURRENT_USER \\ Software \\ Microsoft \\ Internet Explorer \\主路径应包含带有0值的键TabProcGrowth。

String currentWindowHandle = driver.getWindowHandle();
        driver.findElement(By.cssSelector(locator)).click();
        Set<String> windows = driver.getWindowHandles();
        for (String window : windows) {
            if (!window.equals(currentWindowHandle)) {
                driver.switchTo().window(window);
                driver.close();
            }
        }
        driver.switchTo().window(currentWindowHandle);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM