Selenium WebDriver windows switching issue in Internet Explorer 8-10

后端 未结 3 1010
情话喂你
情话喂你 2021-01-13 14:47

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 pl

相关标签:
3条回答
  • 2021-01-13 14:58
    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);
    
    0 讨论(0)
  • 2021-01-13 15:03

    if it is IE11,modify HKLM_CURRENT_USER\Software\Microsoft\Internet Explorer\Main path should contain key TabProcGrowth with 0 value.

    0 讨论(0)
  • 2021-01-13 15:12

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

    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);
    }
    
    0 讨论(0)
提交回复
热议问题