- Jan 20, 2020
- admin
- 0
Handle multiple windows in selenium
[Test]
public void HandleMultipleWindows()
{
dr.Navigate().GoToUrl("https://demoqa.com/browser-windows");
string windowhandleParent = dr.CurrentWindowHandle;
IWebElement btnNewwindow = dr.FindElement(By.XPath("//button[@id='windowButton']"));
btnNewwindow.Click();
System.Collections.ObjectModel.ReadOnlyCollection<string> lstWindow = dr.WindowHandles;
foreach (var handle in lstWindow)
{
Console.WriteLine(handle);
}
//Switching the driver to 2nd window
dr.SwitchTo().Window(lstWindow[1]);
IWebElement sampleText = dr.FindElement(By.XPath("//h1[@id='sampleHeading']"));
Assert.IsTrue(sampleText.Displayed,"Sample text is not displayed");
dr.SwitchTo().Window(windowhandleParent); //return to parent window
Console.WriteLine(dr.Title);
}