How to Handle Multiple windows in Selenium



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);
    }
Tags: ,
Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!

https://bridgejunks.com/ https://crownmakesense.com/ https://brithaniabookjudges.com/ https://hughesroyality.com/ https://rhythmholic.com/ https://bandar89.simnasfikpunhas.com/ https://www.100calshop.co.il/products/thailand/ https://myasociados.com/ https://solyser.com/ http://konfidence.cz/ https://muscadinepdx.com/ https://bandar89.parajesandinos.com.ve/ https://goremekoop.com/ https://oncoswisscenter.com/ https://www.turunclifehotel.com/bandar89/ https://www.houseofproducts.biz/ https://taimoormphotography.com/
BIJI18 BIJI18 BIJI18