How to Handle Multiple Windows in Selenium WebDriver
H Y R Tutorials
@hyrtutorialsAbout
Welcome to my channel H Y R Tutorials. If you are looking for learning something useful (related to automation testing) with some good explanation, then you are in the right place. In this channel I'm going to share the knowledge on various programming languages, technologies and tools, so please stay tuned to this. π YouTube - https://www.youtube.com/hyrtutorials π Website - https://www.hyrtutorials.com π Facebook - https://www.facebook.com/hyrtutorials π Twitter - https://twitter.com/hyrtutorials π Instagram - https://www.instagram.com/hyrtutorials π Telegram - https://t.me/hyrtutorials π LinkedIn - https://www.linkedin.com/company/hyrtutorials Kindly share this channel and website with your friends and help them as well. π Please Subscribeπ to start learning for FREE now, Also help your friends in learning the best by suggesting this channel.
Latest Posts
Video Description
In this video, I have shown how we can handle Multiple Windows in Selenium WebDriver. Window handles refer to the windows or tabs which were opened by the driver instance in selenium. Every window will have a unique string value to identify and we call that as a handle. Selenium provides two methods to get those handle values. 1) getWindowHandle 2) getWindowHandles ==== Get window handle ===== WebDriver does not make the distinction between windows and tabs. If your site opens a new tab or window, Selenium will let you work with it using a window handle. Each window has a unique identifier that remains persistent in a single session. You can get the window handle of the current window by using: driver.getWindowHandle(); ==== Get window handles ===== You can get the window handles of the currently opened windows by using: driver.getWindowHandles(); ==== Switching windows or tabs ===== Clicking a link/button that opens in a new window will focus on the new window or tab on the screen, but WebDriver will not know which window the Operating System considers active. To work with the new window you will need to switch to it. If you have only two tabs or windows open, and you know which window you start with, by the process of elimination you can loop over both windows or tabs that WebDriver can see, and switch to the one which is not the original. Syntax: driver.switchTo().window(windowHandle); Code: //launches browser window with the mentioned URL driver.get("https://www.hyrtutorials.com/p/window-handles-practice.html"); //Store the ID of the parent window String parentWindow = driver.getWindowHandle(); //Click the link which opens in a new window driver.findElement(By.id("newTabBtn")).click(); //Loop through until we find a new window handle for (String windowHandle : driver.getWindowHandles()) { if(!parentWindow.contentEquals(windowHandle)) { driver.switchTo().window(windowHandle); driver.findElement(By.id("firstName")).sendKeys("yadagiri"); break; } } ==== Closing a window or tab ==== When you are finished with a window or tab and it is not the last window or tab open in your browser, you should close it and switch back to the window you were using previously. Assuming you followed the code sample in the above section you will have the previous window handle stored in a variable. Put this together and you will get: //Close the tab or window driver.close(); //Switch back to the old tab or window driver.switchTo().window(parentWindow); ============================================== ************** Checkout my other playlists ************** ============================================== Java Programming videos playlist:π π https://bit.ly/3keRJGa Selenium WebDriver with Java videos playlist:π π https://bit.ly/2FyKvxj Selenium interview questions videos playlist:π π https://bit.ly/3matUB3 Windows automation with FlaUI videos playlist:π π https://bit.ly/33CG4dB CSS Selectors videos playlist:π π https://bit.ly/2Rn0IbD XPath videos playlist:π π https://bit.ly/2RlLdkw Javascript Executor videos playlist:π π https://bit.ly/2FhNXwS Apache POI videos playlist:π π https://bit.ly/2RrngrH Maven videos playlist:π π https://bit.ly/2DYfYZE How to fix Eclipse errors:π π https://bit.ly/3ipvNYf ============================================== ============================================== Connect us @ π Website - www.hyrtutorials.com π Facebook - www.facebook.com/HYRTutorials π LinkedIn - www.linkedin.com/company/hyrtutorials π Twitter - www.twitter.com/Hyrtutorials ============================================== ============================================== π Please Subscribeπ to start learning for FREE now, Also help your friends in learning the best by suggesting this channel. #selenium #windowHandles #multipleWindows #hyrtutorials #webAutomation
Start Your Origami Journey
AI-recommended products based on this video

Art Kit, 272 Pack Art Set Drawing Kit for Kids Girls Boys, Deluxe Gift Art Supplies with Trifold Easel, Origami Paper, Coloring Pad, Sketch Pad, Pastels, Crayons, Pencils, Watercolors (Pink)

Paper Cats Origami Kit, 50pcs DIY 3D Origami Animal Kit for Create Adorable Paper Cats, Creative Craft Activity for Cat Lovers, Fun Home Desk Decor, Christmas Birthday Gift(Cat)

Art Kit, 272 Pack Art Set Drawing Kit for Kids Girls Boys, Deluxe Gift Art Supplies with Trifold Easel, Origami Paper, Coloring Pad, Sketch Pad, Pastels, Crayons, Pencils, Watercolors (Pink)

Paper Cats Origami Kit, 50pcs DIY 3D Origami Animal Kit for Create Adorable Paper Cats, Creative Craft Activity for Cat Lovers, Fun Home Desk Decor, Christmas Birthday Gift(Cat)

Art Kit, 272 Pack Art Set Drawing Kit for Kids Girls Boys, Deluxe Gift Art Supplies with Trifold Easel, Origami Paper, Coloring Pad, Sketch Pad, Pastels, Crayons, Pencils, Watercolors (Pink)

Paper Cats Origami Kit, 50pcs DIY 3D Origami Animal Kit for Create Adorable Paper Cats, Creative Craft Activity for Cat Lovers, Fun Home Desk Decor, Christmas Birthday Gift(Cat)

Art Kit, 272 Pack Art Set Drawing Kit for Kids Girls Boys, Deluxe Gift Art Supplies with Trifold Easel, Origami Paper, Coloring Pad, Sketch Pad, Pastels, Crayons, Pencils, Watercolors (Pink)

Paper Cats Origami Kit, 50pcs DIY 3D Origami Animal Kit for Create Adorable Paper Cats, Creative Craft Activity for Cat Lovers, Fun Home Desk Decor, Christmas Birthday Gift(Cat)

Art Kit, 272 Pack Art Set Drawing Kit for Kids Girls Boys, Deluxe Gift Art Supplies with Trifold Easel, Origami Paper, Coloring Pad, Sketch Pad, Pastels, Crayons, Pencils, Watercolors (Pink)

Art Kit, 272 Pack Art Set Drawing Kit for Kids Girls Boys, Deluxe Gift Art Supplies with Trifold Easel, Origami Paper, Coloring Pad, Sketch Pad, Pastels, Crayons, Pencils, Watercolors (Pink)



















