If you want to close the current active window and pass the control to another window using selenium. Below is the java code that will work for you.
String currentWindow = driver.getWindowHandle();
// switch to first window that is not equal to the current window
String newWindow = null;
for ( String handle : driver.getWindowHandles()) {
if (!currentWindow.equals(handle)) {
newWindow = handle;
break;
}
}
// if there's another window found...
if (newWindow != null) {
// close the current window
driver.close();
// ...switch to the new window
driver.switchTo().window(currentWindow);
}
String currentWindow = driver.getWindowHandle();
// switch to first window that is not equal to the current window
String newWindow = null;
for ( String handle : driver.getWindowHandles()) {
if (!currentWindow.equals(handle)) {
newWindow = handle;
break;
}
}
// if there's another window found...
if (newWindow != null) {
// close the current window
driver.close();
// ...switch to the new window
driver.switchTo().window(currentWindow);
}
Comments
Post a Comment