Monday, 17 March 2014

How to handle multiple browser windows in Selenium Webdriver in C#.Net?

We can use 2 properties to handle multiple browser windows in Selenium Webdriver.
  1. CurrentWindowHandle -
  2. WindowHandles - gets the collection of all open windows

Complete example in C#.Net to handle multiple windows is given below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
using System.Collections.ObjectModel;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Interactions.Internal;
using OpenQA.Selenium.Support.UI;

namespace Abc
    class Program
        static void Main(string[] args)

IWebDriver driver=null;

   driver = new ChromeDriver(@"F:\selenium\csharp");
   driver.Url = "";



String popWindowHandle = "";
//get the current window handles
String mainWindow = driver.CurrentWindowHandle;

//get the collection of all open windows
ReadOnlyCollection<string> windowHandles = driver.WindowHandles;
foreach (string handle in windowHandles)
if (handle != mainWindow)
newWindowHandle = handle;
//switch to new pop up window
// and perform any operation you want to perform
//Print the title of new pop up window just opened.
//switch back to original window
} catch(Exception e){ Console.WriteLine("Exception ******"+e.ToString()); } finally{ Thread.Sleep(2000); driver.Quit(); Console.ReadLine(); } } } }
What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at

No comments:

Post a Comment

Buy Best Selenium Books