I've been attempting to figure this out for the past two days, unfortunately with no luck. I'm currently automating a scenario where a new window for a form is popped up from the main application under test. Unfortunately, the page with the originating link, and the popup page has the same title.

The code that I'm currently using looks as follows:

string MainWindowHandle = browser.GetWindowHandle();
ReadOnlyCollection<string> handles = browser.GetWindowHandles();
string newWindowHandle = string.Empty;
foreach (string handle in handles)
    if(handle != mainWindowHandle)
      newWindowHandle  handle.ToString();

Is there another way that I should be doing this, or am I going to need to discuss the possibility of having one of the titles changed by a character?

2 Answers 2


Sounds like an odd bug, I thought the window handle used to be a hash like object and you could use either the handle or the title of the window to switch to and from but that might have changed since the last time I saw it. If you want both windows to have the same name you could use the driver to execute JavaScript that will grab the element creating the window and have it change the title of the window only. This way if the JavaScript ever changes the test won't immediately break. It's not the best solution; I hate running JavaScript in selenium tests, but every so often it is the easiest/fastest solution.

  • The handle is normally a hash. The handles are different in this case (don't have my tests with me at the moment, but, ie: internet_explorer_browser_6 with the pop up being internet_explorer_browser_8). I'm not familiar with injecting javascript to change an element through selenium. Would you happen to have any references where I could begin to start looking? Jun 30, 2011 at 9:39
  • Sure, for a basic overview there is some at Selenium FAQ and for a more in-depth explanation you can use JavascriptExectutor API.
    – Jason Ward
    Jun 30, 2011 at 14:52
  • Cool. Thank you! I'll take a look into it. Jun 30, 2011 at 14:57
  • Back before Selenium 2 could deal with alerts we used to have to call self.driver.execute_script('window.onbeforeunload = function(){};') (sorry its python bindings) in order to prevent the alert from showing up when we were trying to change pages without saving. Its a simple example, but I feel you will be able to do something similar
    – Jason Ward
    Jun 30, 2011 at 18:21
  • Haven't had a chnace to touch this project since asking this, however, I've used this technique in a few other circumstances since and it seems as though it will work. Jul 19, 2011 at 9:54

If the popup window is generated by execution of javascript on the parent window, there used to be an issue with Selenium that it would not identify the newly generated window. If you haven't tried, the first thing I would do is check that the Selenium / WebDriver even knows that a second window exists.

  • Although it is generated by javascript, when I do the same thing with another popup that is generated the same way, but has a title different than the originating window, it seems to work fine. Jun 24, 2011 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.