Switched to IE11 window times out [switchTo().window(handle)]
Meta -
OS: Windows 7 Selenium Version: 2.53 Browser: IE Browser Version: 11
Expected Behavior -
I am using C# code to change IE11 browser's tab switch. so far i can open a new tab successfully, but when i switch the tab using the following command, my code gets stuck :
browser.driver.switchTo().window(windowHandles.oldTab)
Is it a Selenium Bug, or is this the wrong syntax to Switch Window Handle?
when i debug the issue, i get the following exception after waiting a long time :
An exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.dll but was not handled in user code
Additional information: The HTTP request to the remote WebDriver server for URL http://127.0.0.1:4444/wd/hub/session/36fa4074-3b34-4dea-9da8-c8278b135c4f/window timed out after 240 seconds.
The issue is described in detail here, Please take a look at last 2 comment in this thread : https://github.com/angular/protractor/issues/55
Actual Behavior -
The driver object should Switch WindowsHandle. instead it stops at code line browser.driver.switchTo().window(windowHandles.newTab);
Steps to reproduce -
The code example is also written in the link.
If your code is the same as that in the other issue, then the code is incorrect. There is a common misconception that the call to getWindowHandles returns window handles in some order. That is, there is a false expectation that handles[0] is the handle to the original window and handles[1] will be the popup. This is explicitly not the case. The order of handles is undefined.
My latest code is as following, i have the correct handle in "this.WindowHandle". :
public Boolean SwitchToMyWindow()
{
try
{
String windowHandle = this.WindowHandle;
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}", windowHandle);
// this line has the issue
this.Driver.SwitchTo().Window(windowHandle);
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;
}
Hi @jimevans
I'm assuming there is some code missing due to which you could not answer so I'm pasting my entire logic here again.
public void CreateBlankExperimentFromExperimentView()
{
//Start from Library view
Library library = DoLogin<Library>();
library.WaitUntilReady();
Pause.seconds(2);
Experiment experiment = TriggerWindow.ByClick<Experiment>(PanelSliderNew.CreateNewDocument.BlankExperiment);
experiment.WaitUntilReady();
Assert.GreaterOrEqual(experiment.Editor.Sections.Count, 4, "Experiment has less sections than expected");
}
public static TResult ByClick<TResult>(PageObject pageObject, int timeout = 30) where TResult : PageObject, new()
{
Action action = pageObject.Click;
var page = TriggerAndWaitForNewWindow<TResult>(pageObject, action, timeout); // function definition is given below
if (page == null)
{
throw new Exception("No new window appeared after Click on {0}".Params(pageObject.Alias));
}
return page;
}
}
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(); // this function clicks on the button that will open the new page
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[0].Length > 1;
if (hasSomeLength)
foundHandle = differentHandles[0];
}
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;
// Switch driver focus to the new window
page.SwitchToMyWindow(); // function definition is given below
return page;
}
public Boolean SwitchToMyWindow()
{
try
{
//if (this.driver.BrowserFamily == Browser.Family.InternetExplorer)
//{
// //logger.Info("IE specific, Setting Page Load Timeout to 4 seconds");
// this.driver.Manage().Timeouts().SetPageLoadTimeout(new TimeSpan(0, 0, 0, 4));
//}
//logger.Info("{0} : Switching to window {1}", this.alias, this.windowHandle);
String windowHandle = this.WindowHandle;
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}", windowHandle);
System.Threading.Thread.Sleep(1000);
this.Driver.WindowHandle = windowHandle;
this.Driver.SwitchTo().Window(windowHandle);
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;
}`
So this is the entire logic. I have tried all IEDriver version from 2.39 to 2.53.0. Our tests work fine on all environments including chrome, firefox, safari (win 7, win 8, win10) but IE has this issue that it halts completely and doesn't recover once it hits the new page and tries to find elements. On chrome and firefox a new tab is opened but IE opens a new page. Your time and help is greatly appreciated.
No, the reason that I haven't replied is that as an unpaid volunteer, Selenium project issues don't always rate as my highest priority. Your patience and understanding is greatly appreciated.
Having said that, I'm not sure how much more help I can be without a sample page to test against. Switching windows is not globally broken for all sites with the IE driver. The Selenium project's test suite has a fair number of tests around this exact scenario, and those pass without issue. You might provide a log from IEDriverServer.exe, preferably one captured at the TRACE level, but without a sample page, that only has a marginal chance of being able to pinpoint the root cause.
I understand, thank you for your help. I've written a workaround to avoid the troubling code. you can close this issue.
I'll close the issue, but it might be interesting and useful to see what the workaround is.
Hi @sophiaatif will it be possible to share the workaround i am facing the same issue with IE 11
I am having a similar "hanging" issue and I am making sure I am not trying to switch to the window I am on. Either way one would expect switching to the current window would be a noop server side. I am using the python bindings for the record.
Hey @sophiaatif, I would be interested in seeing your workaround as well.
Hey guys im so sorry for not replying and the main reason is that i left that job and I couldn't recall the solution, but I can vaguely recall that the issue was in logic, perhaps I was trying to access the new page before it got created or something like that, though now that i see the code here it doesnt look like that but the issue was only in logic and I corrected the order of calls. I hope it helps?