- Jan 21, 2019
- admin
- 0
How to Handle Multiple Tabs in Selenium
[Test]
public void HandleMultipleTabs()
{
dr.Navigate().GoToUrl("https://demoqa.com/browser-windows");
string windowhandleParent = dr.CurrentWindowHandle; //getting parentwindow handle
IWebElement btnNewwindow = dr.FindElement(By.XPath("//button[@id='tabButton']"));
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.Navigate().GoToUrl("http://google.com");
dr.SwitchTo().Window(windowhandleParent); //return to parent window
Console.WriteLine(dr.Title); //get the parent window title and print
}