Sophia Sabir
Sophia Sabir

Reputation: 33

Selenium Script halts while switching window handles

Im facing exactly the same issue as described here

but its a closed thread. Im using selenium webdriver 2.48.2, on win7 IE 11. The situation goes like this, I have a test that clicks on a button which is supposed to open a new experiment, this new experiment opens in new tab on chrome and in the same tab on firefox, but opens in new window on IE11 when it is run through selenium. But strange thing is that it does not open in new window when the browser was opened manually instead of through selenium script. maybe the selenium script opens new webdriver? and script halts while searching for new page's elements. what the code does is, it checks if the new handle was opened or not, finds the new handle and then switches the window handle to newer one. Here is the c# code snippet.

 private static TResult TriggerAndWaitForNewWindow<TResult>(PageObject pageObject, Action action, int timeout = 30)
  where TResult : PageObject, new()
{
  IParent parent = pageObject.Driver;
  List<String> existingHandles = pageObject.Driver.WindowHandles.ToList();
  action();

  string popupHandle = Wait.Until(() =>
  {
    string foundHandle = null;
    List<string> currentHandles = pageObject.Driver.WindowHandles.ToList();
    var differentHandles = GetDifference(existingHandles, currentHandles);

    if (differentHandles.Count > 0)
    {
      Boolean hasSomeLength = differentHandles[differentHandles.Count-1].Length > 1;

      if (hasSomeLength)
        foundHandle = differentHandles[differentHandles.Count - 1];      
    }
    return foundHandle;
  }, "Waiting for new Window Handle to appear", timeout, 2000);

  // Init the new page object but override the window handle
  TResult page = PageObject.Init<TResult>(parent);
  page.WindowHandle = popupHandle;

  page.SwitchToMyWindow();
  return page;
}

private static List<String> GetDifference(List<string> existingHandles, List<string> currentHandles)
{
        System.Threading.Thread.Sleep(15000);
        return currentHandles.Except(existingHandles).ToList();
}

Halts inside this function on IE11

public Boolean SwitchToMyWindow()
{    
  try
  {
    String windowHandle = this.WindowHandle; // must be the old handle
    try
    {
      if (this.Driver.CurrentWindowHandle == windowHandle)
      {
        Log.Info("No need to cswitch window");
        return true;
      }
    }
    catch(Exception e)
    {
      Log.Warn("We have no current driver window, must have been closed");
    }

    Log.Info("Switching to Window Handle {0}", this.Driver.CurrentWindowHandle);
    this.Driver.SwitchTo().Window(windowHandle); <---- Halts here on IE11
    //Pause.milliSeconds(500);
            Boolean switched = Wait.Until(() =>
              this.Driver.CurrentWindowHandle == windowHandle, "Waiting for my window handle to be the active one", 5, 1000);
        }
  catch (OpenQA.Selenium.WebDriverTimeoutException tEx)
  {

  }
  return true;
}

Did anyone else ever faced this issue? How can resolve it?

Upvotes: 1

Views: 895

Answers (1)

Atif Waqar
Atif Waqar

Reputation: 89

Can you verify if Selenium supports your target OS? It is possible that Selenium is not fully supported on your Target OS.

Please check the following link for more details. http://grokbase.com/t/gg/webdriver/1267fdkgaa/openqa-selenium-nosuchwindowexception-with-ie9-and-windows-2008

Upvotes: 1

Related Questions