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("[ Ссылка ]");
//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:👇
🔗 [ Ссылка ]
Selenium WebDriver with Java videos playlist:👇
🔗 [ Ссылка ]
Selenium interview questions videos playlist:👇
🔗 [ Ссылка ]
Windows automation with FlaUI videos playlist:👇
🔗 [ Ссылка ]
CSS Selectors videos playlist:👇
🔗 [ Ссылка ]
XPath videos playlist:👇
🔗 [ Ссылка ]
Javascript Executor videos playlist:👇
🔗 [ Ссылка ]
Apache POI videos playlist:👇
🔗 [ Ссылка ]
Maven videos playlist:👇
🔗 [ Ссылка ]
How to fix Eclipse errors:👇
🔗 [ Ссылка ]
==============================================
==============================================
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
How to Handle Multiple Windows in Selenium WebDriver
Теги
hyr tutorialsh y r tutorialsselenium webdriverautomation testinghow to handle multiple windows or tabs in selenium webdriverhandling multiple windows in seleniumhandling multiple windows in selenium webdriverhandling multiple windows in selenium webdriver javahandling multiple windows in selenium javaselenium multiple windows javaselenium multiple windowsselenium tutorial for beginnersselenium webdriver tutorialselenium tutorialseleniumwebdriverjava