Handle Multiple Windows using selenium:
- Selenium WebDriver provides an alphanumeric ID for each window as soon as the WebDriver object is given instantiated.
- This unique alphanumeric ID is called window handle id in Selenium.
- This unique ID uses to switch control between multiple windows.
- In simple words, each unique window has a unique ID, so that selenium can differentiate when it is switching from one window to another.
GetWindowHandle Command:
Purpose: To get the window handle of the current window. Store current window ID into String object.
- When we handle to Window first time we have need to store current window because of after perform any operation we need to switch back into parent window.
1 | String handle= driver.getWindowHandle(); |
GetWindowHandles Command:
Purpose: To get the window handle of all the currently open windows.
- Store all currently open windows into String Set. Store all window’s to perform operations on different tab’s at a time.
1 | Set<String> handle= driver.getWindowHandles(); |
SwitchTo Window Command:
Purpose: WebDriver supports moving between named windows using the “switchTo” method.
- Switch WebDriver control between one Window to Another. To perform operations on windows we have need to switch between multiple tabs.
1 | driver.switchTo().window("windowName"); |
Or
Alternatively, you can pass a “window handle” to the “switchTo().window()” method to switch between multiple window’s. It is possible to iterate over every open window like this:
1 2 3 4 5 | for (String handle : driver.getWindowHandles()) { driver.switchTo().window(handle); } |
SwitchTo Frame Command:
Purpose: WebDriver can be move between named frames using the “switchTo” method from one frame to other. Go through in Details SwitchToFrame click on this Link:
1 | driver.switchTo().frame("frame_Name"); |
Scenarios of Switch one window to other window:
- Current window location store into Parent Window string.
- Click some link that opens a new window.
- Store newly open window into Child Window using getWindowHandles.
- Switch to Newly open window using switchTo().
- Code to do something on new window.
- Close newly opened window when done with it.
- switch back to the Parent window.
Example of Handle Newly open Window:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Window_Handle { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Current window location store into Parent Window string. String parentWindow = driver.getWindowHandle(); // Click some link that opens a new window. driver.findElement(By.xpath("//*[@id='window_path']")).click(); // Store newly open window into Child Window using getWindowHandles. for (String childWindow : driver.getWindowHandles()) { // Switch to Newly open window using switchTo(). driver.switchTo().window(childWindow); } // Close newly opened window when done with it. driver.close(); // switch back to the Parent window. driver.switchTo().window(parentWindow); } } |
Handle Multiple Windows using selenium