There are many applications,where a link display on new window or tab when you click on it.
In that situation we have to switch driver focus from parent to child window and vice versa
In this post, I will explain how can we handle multiple windows using window Handlers in selenium webdriver.
Following reusable methods will help you for framework design.
//Handling New window(tab) from Parent window(tab)
public static void handleNewTab(WebDriver driver)
{
Set<String> allWindowHandles = driver.getWindowHandles();
Iterator<String> iter=allWindowHandles.iterator();
int size=allWindowHandles.size();
String child = null;
for(int i=0;i<size;i++){
child=iter.next();
}
driver.switchTo().window(child);
}
//Handling Parent window(tab) from child window(tab)
public static void handleParentTab(WebDriver driver){
Set<String> allWindowHandles = driver.getWindowHandles();
String parent = (String) allWindowHandles.toArray()[0];
driver.switchTo().window(parent);
}
When user is working on more than two tabs(windows), following method will help if he wants to switch to second window(tab) from child tab(third window).
//Handling child Parent window(tab)
public static void handleChildParentTab(WebDriver driver){
Set<String> allWindowHandles = driver.getWindowHandles();
String window1 = (String) allWindowHandles.toArray()[1];
driver.switchTo().window(window1);
}
In that situation we have to switch driver focus from parent to child window and vice versa
In this post, I will explain how can we handle multiple windows using window Handlers in selenium webdriver.
Following reusable methods will help you for framework design.
//Handling New window(tab) from Parent window(tab)
public static void handleNewTab(WebDriver driver)
{
Set<String> allWindowHandles = driver.getWindowHandles();
Iterator<String> iter=allWindowHandles.iterator();
int size=allWindowHandles.size();
String child = null;
for(int i=0;i<size;i++){
child=iter.next();
}
driver.switchTo().window(child);
}
//Handling Parent window(tab) from child window(tab)
public static void handleParentTab(WebDriver driver){
Set<String> allWindowHandles = driver.getWindowHandles();
String parent = (String) allWindowHandles.toArray()[0];
driver.switchTo().window(parent);
}
When user is working on more than two tabs(windows), following method will help if he wants to switch to second window(tab) from child tab(third window).
//Handling child Parent window(tab)
public static void handleChildParentTab(WebDriver driver){
Set<String> allWindowHandles = driver.getWindowHandles();
String window1 = (String) allWindowHandles.toArray()[1];
driver.switchTo().window(window1);
}