Question: So I'm working with selenium firefox webdrivers in c# winform and I have this code below to get the handle of the popup that shows when you click on the "webtraffic\_popup\_start\_button" and it should get the handle of the popup but the popup handle is same as current one.

```
string current = driver.CurrentWindowHandle;
driver.FindElement(By.XPath("//*[@id='webtraffic_popup_start_button']")).Click();
Thread.Sleep(Sleep_Seconds);
popup = driver.CurrentWindowHandle;
Thread.Sleep(3000);
driver.SwitchTo().Window(current);
Thread.Sleep(1000);

```

Any help with this would be much appreciated thank you

This is what pop up looks like.

[![Popup_Image](https://i.stack.imgur.com/TPoaz.png)](https://i.stack.imgur.com/TPoaz.png)

Answer: 
If the lastly opened window is your target then simply do the following after the click

```
driver.SwitchTo().Window(driver.WindowHandles.ToList().Last());

```

**EDIT**

```
//You may need to go back to parent window to perform additional actions;

// to the new window
driver.SwitchTo().Window(driver.WindowHandles.ToList().Last());

 // to the new window
driver.SwitchTo().Window(driver.WindowHandles.ToList().First());
//or
driver.SwitchTo().DefaultContent();

```
I've got some code you might like. The quickest solution is to use Popup Finder, but I've made my own method as well. I would never rely on the order the Window Handles are in to select the appropriate window. Popup Window Finder:

```
PopupWindowFinder finder = new PopupWindowFinder(driver);
driver.SwitchTo().Window(newWin); 

```

My Custom method. Basically you pass it the element you want to click, your webdriver, and optionally the time to wait before searching after you click the element.

It takes all of your current handles and makes a list. It uses that list to eliminate the previously existing windows from accidentally getting switched to. Then it clicks the element that launches the new window. There should always be some sort of a delay after the click, as nothing happens instantly. And then it makes a new list and compares that against the old one until it finds a new window or the loop expires. If it fails to find a new window it returns null, so if you have an iffy webelement that doesn't always work, you can do a null check to see if the switch worked.

```
public static string ClickAndSwitchWindow(IWebElement elementToBeClicked,
IWebDriver driver, int timer = 2000)
        {
            System.Collections.Generic.List<string> previousHandles = new 
System.Collections.Generic.List<string>();
            System.Collections.Generic.List<string> currentHandles = new 
System.Collections.Generic.List<string>();
        previousHandles.AddRange(driver.WindowHandles);
        elementToBeClicked.Click();

        Thread.Sleep(timer);
        for (int i = 0; i < 20; i++)
        {
            currentHandles.Clear();
            currentHandles.AddRange(driver.WindowHandles);
            foreach (string s in previousHandles)
            {
                currentHandles.RemoveAll(p => p == s);
            }
            if (currentHandles.Count == 1)
             {
                driver.SwitchTo().Window(currentHandles[0]);
                Thread.Sleep(100);
                return currentHandles[0];
            }
            else
            {
                Thread.Sleep(500);
            }
        }
        return null;
    }

```