top of page

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 site

Example_1: Working with multiple windows

[TestMethod, TestCategory("AgileQualityBlog")]
public void MultipleWindows()
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

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);
Console.WriteLine("\nParent window is located in index 0: " + ParentWindow);


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 

//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]);


//Switch to the new tab (located in index '1')

//Wait untill the new window is opened

//Search for specific string in the new window 

//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

//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(""));

133 views0 comments
bottom of page