Selenium C# Tutorial: Switch Between Multiple Tabs or Windows
We may encounter a typical scenario when interacting with a web application by opening up a new window (or tab) when clicking a button. The use of multiple windows can be automated using Selenium WebDriver by the "window handle," which is a unique id assigned to each window ("CDwindow-09CF8151C6EBF7FB5453B5AEAC70D85E" ) that we can use as an input to the SwitchTo() method used to switch between different windows (or tabs).
When opening a new window or a tab, the Selenium web driver keeps track of how many windows it opened during a session. This means that it will keep this information only for the windows opened within the current session of selenium WebDrive and not Opened by a previous session of Selenium Webdriver.
In this tutorial, we will learn how we can work with more than one window or more than one tab.
Commands Used For Window Handling
The commands below are vital for switching to a new window (or tab), knowing the info about the current window, and all the other windows open.
Note: All code examples are based on the https://testing.todorvachev.com/ site

Example_1: Working with multiple windows
[TestMethod, TestCategory("AgileQualityBlog")]
public void MultipleWindows()
{
driver.Navigate().GoToUrl("https://testing.todorvachev.com/new-tab/");
string openNewTab = "//*[@id=\"post-182\"]/div/p[1]/a[1]";
string openNewWindow = "//*[@id=\"post-182\"]/div/p[1]/a[2]";
int counter = 0;
IWebElement newTab = driver.FindElement(By.XPath(openNewTab));
IWebElement newWinow = driver.FindElement(By.XPath(openNewWindow));
//Returns the Window handle of current focused browser window
string ParentWindow = driver.CurrentWindowHandle;
//Open a new window(You can use the "newTab") variable insted
newWinow.Click();
/*
Returns a ReadOnlyCollection (You need to typecast this collection to List Collection by using ToList()) of all Window handles of all the browsers that were opened in the session.
*/
List<string> AvailableWindows = driver.WindowHandles.ToList();
foreach (var handle in AvailableWindows)
{
Console.WriteLine("Window ID #{0}: {1}" , counter,handle);
counter++;
}
Console.WriteLine("\nParent window is located in index 0: " + ParentWindow);
}
Output:

Example_2: Working with multiple Tabs with validation
[TestMethod, TestCategory("AgileQualityBlog")]
public void MultipleTabs()
{
Driver_Wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
//New Tab element using Xpath
string openNewTab = "//*[@id=\"post-182\"]/div/p[1]/a[1]";
//Locating the NewTab window
IWebElement newTab = driver.FindElement(By.XPath(openNewTab));
//Store the parent window into a variable for further use
string ParentWindow = driver.CurrentWindowHandle;
//Open a new Tab
newTab.Click();
//driver.WindowHandles is a ReadOnlycollection So we will use the '.ToList()' and store into the 'List<string>'
List<string> AvailableTabs = driver.WindowHandles.ToList();
//Print all windows currently available
for (int i = 0; i < AvailableTabs.Count; i++)
{
Console.WriteLine("Window handle in location {0} is {1}", i, AvailableTabs[i]);
}
//System.Threading.Thread.Sleep(5000);
//Switch to the new tab (located in index '1')
driver.SwitchTo().Window(AvailableTabs[1]);
//Wait untill the new window is opened
Driver_Wait.Until(ExpectedConditions.UrlContains("https://www.google.com/"));
//Search for specific string in the new window
driver.FindElement(By.Name("q")).SendKeys("agilequalitymadeeasy");
driver.FindElement(By.Name("q")).Submit();
//Validate search result
IWebElement searchResult = driver.FindElement(By.XPath("//*[@id=\"rso\"]/div[1]/div/div/div[1]/a/h3"));
Assert.AreEqual("Quality Assurance | AgileQualityMadeEasy", searchResult.Text);
//Close Tab
driver.Close();
//At this point there is no focused window, we have to explicitly switch back to some window.
driver.SwitchTo().Window(ParentWindow); //or AvailableTabs[1])
//Validate URL Driver_Wait.Until(ExpectedConditions.UrlContains("https://testing.todorvachev.com/new-tab/"));
}