HANDLING MULTIPLE WINDOWS IN WEBDRIVER

Selenium WebDriver Switch Window Commands
Some web applications have many frames or multiple windows. Selenium WebDriver a
ssigns an alphanumeric id to each window as soon as the WebDriver object is inst
antiated. This unique alphanumeric id is called window handle. Selenium uses thi
s unique id to switch control among several windows. In simple terms, each uniqu
e window has a unique ID, so that Selenium can differentiate when it is switchin
g controls from one window to the other.

GetWindowHandle Command
Purpose: To get the window handle of the current window.
String handle= driver.getWindowHandle();//Return a string of alphanumeric wind
ow handle
String handle= driver.getWindowHandle();//Return a string of alphanumeric wind
ow handle
GetWindowHandles Command
Purpose: To get the window handle of all the current windows.
Set<String> handle= driver.getWindowHandles();//Return a set of window handle
Set<String> handle= driver.getWindowHandles();//Return a set of window handle
SwitchTo Window Command
Purpose: WebDriver supports moving between named windows using the
.

switchTo

method

driver.switchTo().window("windowName");
driver.switchTo().window("windowName");
Or
Alternatively, you can pass a window handle to the switchTo().window()
ng this, it s possible to iterate over every open window like so:

method. Knowi

for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);}
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);}
Or
Switching between windows with Iterators:
driver.findElement(By.id( id of the link which opens new window )).click();

//wait till two windows are not opened waitForNumberofWindowsToEqual(2).switchTo().iterator(). SwitchTo Frame Command Purpose: WebDriver supports moving between named frames using the switchTo method. This interface work . if (winHandle!=firstWinHandle){ //To retrieve the handle of second window.next().id( id of the link which opens new window )).window(secondWinHandle). extracting the handle which does not match to first window handle secondWinHandle=winHandle. firstWinHandle = driver.switchTo(). extracting the handle which does not match to first window handle secondWinHandle=winHandle.frame("frameName").remove(firstWinHandle).getWindowHandle(). driver.iterator(). firstWinHandle = driver.click(). read its contents or even type into a prompt. After you ve triggered an action that opens a popup. if (winHandle!=firstWinHandle){ //To retrieve the handle of second window. handles.switchTo().next().//this method is for wait Set handles = driver.findElement(By. you can access the alert and it will return the currently open alert object. String winHandle=handles.getWindowHandles(). //wait till two windows are not opened waitForNumberofWindowsToEqual(2).getWindowHandles(). //Storing handle of second window handle //Switch control to new window driver. String winHandle=handles.frame("frameName").getWindowHandle(). driver.switchTo().window(secondWinHandle).//this method is for wait Set handles = driver. //Storing handle of second window handle //Switch control to new window driver.remove(firstWinHandle). driver. handles. dismiss. SwitchTo PopUp Command Purpose: WebDriver supports moving between named PopUps using the switchTo method. With this object you can now ac cept.

xpath("//div[@id='monthDropDown']//div")).out.window(Child_Window).get(1).click().click(). Alert alert = driver. } @After public void close() { driver.click().xpath("//img[@alt='Calender']")).size().switchTo().selenium.alert().getWindowHandle().switchTo(). Alert alert = driver.get(URL). } //Switching back to Parent Window driver. String URL="https://www. //Performing some actions on Parent Window driver.xpath("//*[@id='calendarDiv']/div/table/tbody/tr/td[conta ins(text().selenium.openqa. // Performing actions on child window driver.click(). confirms.getWindowHandles()) { driver.className("btn_style")).id("calendar_month_txt")). driver. } @Test public void test() throws Exception { // Opening Calender driver. import java.switchTo().co. } } ------------------------package practiceTestCases.alert(). @Before public void setup() throws Exception { driver=new FirefoxDriver(). import org.concurrent.findElement(By.maximize().abc.manage().By. int Months_Size=Months.println("Month size is:"+Months_Size). // Storing parent window reference into a String Variable String Parent_Window = driver. and prompts. import java. import org. .WebDriver.findElement(By. driver.quit().switchTo().window(). Months.TimeUnit.'16')]")).findElement(By.findElement(By.openqa.click(). System. driver. List Months=driver.util. // Switching from parent window to child window for (String Child_Window : driver. public class MultiWindowHandle { WebDriver driver.in/".findElements(By.util.s equally well on alerts.Set.window(Parent_Window).

out. public class PracticeSwitchWindow { public static WebDriver driver. // Pass a window handle to the other window for (String handle1 : driver. // Click on the Button "New Message Window" driver.get("http://toolsqa.out. System.FirefoxDriver.getWindowHandles()) { System.import org. public static void main(String[] args) { // Create a new instance of the Firefox driver driver = new FirefoxDriver(). } // Closing Pop Up window driver. // Store and Print the name of the First window on the console String handle= driver. } . // Close Original window driver.close().manage().switchTo().name("New Message Window")).openqa.out. this means that any search for elements on the page could take the time the implicit wait is set for before throwing ex ception driver.quit().timeouts().click().com/automation-practice-swit ch-windows/").getWindowHandle().println(handle).SECONDS).println(handles). System.firefox. // Store and Print the name of all the windows open Set handles = driver. // Put an Implicit wait. driver.findElement(By.selenium.window(handle1).wpengine.println(handle1). // Launch the URL driver. TimeUnit.getWindowHandles().implicitlyWait(10.

name("Alert Box")).Set.selenium. // Switch to JavaScript Alert window Alert myAlert = driver. import org.FirefoxDriver.openqa.accept().WebDriver. // Put an Implicit wait. public class PracticeSwitchWindow { public static WebDriver driver. import java. public static void main(String[] args) { // Create a new instance of the Firefox driver driver = new FirefoxDriver().manage(). // Launch the URL driver.implicitlyWait(10.findElement(By.selenium.Alert.wpengine.} ----------------------package practiceTestCases. // Close Original window driver.By.TimeUnit.click().selenium.util. import org.concurrent.alert().openqa. import org.switchTo().timeouts().openqa. // Click on the Button "Alert Box" driver.SECONDS).get("http://toolsqa.close(). } } . this means that any search for elements on the page could take the time the implicit wait is set for before throwing ex ception driver. // Accept the Alert myAlert. import org.firefox.openqa. TimeUnit.selenium.com/automation-practice-swit ch-windows/").util. import java.

----------------------------------- .