Created
August 10, 2022 00:55
-
-
Save bharathmuddada/be198ea5ef8776909cd99b43c9984b13 to your computer and use it in GitHub Desktop.
C# Selenium Program for window handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SeleniumWindowHandling { | |
public static void Main(string[] args) | |
{ | |
new DriverManager().SetUpDriver(new ChromeConfig()); | |
var driver = new ChromeDriver(); | |
driver.Manage().Window.Maximize(); | |
driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/windows"); | |
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5); | |
//Store the ID of the original window | |
string originalWindow = driver.CurrentWindowHandle; | |
Console.WriteLine(originalWindow); | |
Console.WriteLine(driver.WindowHandles.Count); | |
driver.FindElement(By.XPath("//a[text()='Click Here']")).Click(); | |
var winhandles = driver.WindowHandles; | |
foreach (string window in winhandles) | |
{ | |
if (originalWindow != window) | |
{ | |
//Switching to new window | |
driver.SwitchTo().Window(window); | |
Console.WriteLine(driver.Title); | |
} | |
} | |
//switching to original window | |
driver.SwitchTo().Window(originalWindow); | |
Console.WriteLine(driver.Title); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment