Internet Explorer 8-10中的Selenium WebDriver Windows切换问题 [英] Selenium WebDriver windows switching issue in Internet Explorer 8-10

查看:178
本文介绍了Internet Explorer 8-10中的Selenium WebDriver Windows切换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现在尝试使用Selenium WebDriver测试我们的应用程序时遇到了问题。问题在于IE9中不稳定的弹出窗口。它并不总是可重现的,它发生在大约20%的窗口切换中,但几乎不可能在IE上进行测试。在FireFox中,一切都很完美。

I found a problem trying to use Selenium WebDriver for testing our application. The issue is in unstable pop-ups focusing in IE9. It is not always reproducible, it takes place in about 20% of windows switching but makes testing on IE almost impossible. In FireFox everything works perfect.


  1. 我尝试增加超时:

TimeSpan interval = new TimeSpan(0,0,10);
driver.Manage()。超时().ImplicitlyWait(interval);


  1. 创建自己的对象查找方法:

  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{}
            }


  • 尝试使用CssSelecotrs

  • Try to use CssSelecotrs

    尝试在找到元素之前进行一些重新开关:

    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句柄正确显示。例如,如果我切换到第三个窗口,driver.CurrentWindowHandle给我第三个窗口的正确句柄。但是如果我试图找到任何元素,FindElement()会抛出异常。页面已加载,我可以手动单击该元素,但FindElement()无法找到它。如果我重新运行测试,则可以毫无问题地通过此步骤,并且仅在下一次切换或更进一步时失败。这是不可预测的。

    Windows handles in debugger displays correctly. For example if I switches to the third window, driver.CurrentWindowHandle gives me correct handle of third window . But if I try to find any element, FindElement() throws me an exception. The page is loaded, I can click the element manually but FindElement() can't find it. 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?

    推荐答案

    随着IE驱动程序,无法保证窗口在集合中的显示顺序。也就是说,集合中的第0个窗口不一定是会话打开的第一个窗口。鉴于这种情况,您需要执行以下操作:

    With the IE driver, the order in which the windows appear in the collection is not guaranteed. That is, the 0th window in the collection is not necessarily the first window opened by the session. 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;
    }
    

    上述函数的用法如下:

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

    这篇关于Internet Explorer 8-10中的Selenium WebDriver Windows切换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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