C Sharp with Selenium - How to Switch one tab to another tab in Csharp Selenium?

SeleniumAutomation TestingTesting Tools

We can switch one tab to another tab with Selenium webdriver in C#.

Sometimes on clicking a link or a button, we can have multiple tabs opened in the same browser.

By default, the webdriver can access only the parent tab. To access the second tab, we have to switch the driver focus with the help of the SwitchTo().Window() method. The window handle id of the tab where we want to switch to is passed as a parameter..

The method CurrentWindowHandle yields the window handle id of the tab which is in focus. The WindowHandles method returns all the window handle ids of the opened tabs in the browser.

Let us try to switch the tabs opened in browser as shown in below image −




using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System;
using OpenQA.Selenium;
namespace NUnitTestProject2{
   public class Tests{
      String url =" https://www.tutorialspoint.com/index.htm";
      IWebDriver driver;
      public void Setup(){
         //creating object of FirefoxDriver
         driver = new FirefoxDriver("");
      public void Test2(){
         //implicit wait
         driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
         //URL launch
         //identify element then click
         IWebElement l = driver.FindElement(By.XPath("//*[text()='Jobs']"));
         //switch to second tab
         //get current window handle id
         ("Current window id: " + driver.CurrentWindowHandle);
         Console.WriteLine("Page title in second tab is: " + driver.Title);
         //close second tab
         //switch to first tab
         IWebElement m = driver.FindElement(By.TagName("h4"));
         Console.WriteLine("Element in first tab is: " + m.Text);
      public void close_Browser(){


Published on 07-Apr-2021 12:16:14