I am writing a selenium script to login and create new mail, send it and logout. But when I click on New mail button, it opens a new window. In selenium how I can handle this. I am new to selenium. Please explain in detail.
-
3Possible duplicate of [how to handle mutliple windows in selenium webdriver ,need to switch from second to third window](https://stackoverflow.com/questions/45455402/how-to-handle-mutliple-windows-in-selenium-webdriver-need-to-switch-from-second) – undetected Selenium Aug 03 '17 at 06:19
3 Answers
Use the below code, you have to use getWindowHandles- I hope it helps, Let me know in case you get stuck anywhere else -
@Test
public void multipleWindows() {
driver.get(URL+"/windows");
driver.findElement(By.cssSelector(".example a")).click();
Object[] allWindows = driver.getWindowHandles().toArray();
driver.switchTo().window(allWindows[0].toString());
Assert.assertNotEquals(driver.getTitle(), "New Window");
driver.switchTo().window(allWindows[1].toString());
Assert.assertEquals(driver.getTitle(), "New Window");
}
}

- 51
- 12
How to get Window Handle:
String handle = driver.getWindowHandle();
If there are multiple window gets opened when you click on any link,button etc... then to get "window Handle" of each window -
Set windowHandles = driver.getWindowHandles();
"getWindowHandles()" : method return window handle (unique id for each opened window) so that the return type is set. Because set will not contain duplicate elements so here getWindowHandles return unique id/window handles for each winodw and stored in Set.
How to switch to correct / appropriate window:
Basically there are two ways to switch to appropriate window.
- By using windowName
driver.switchTo().window( "windowName" );
OR To get current/parent/default window handle:
String handle = driver.getWindowHandle(); driver.switchTo().window( handle ); OR Set < String > windowHandles = driver.getWindowHandles(); for(String handle : windowHandles ) { driver.swicthTo().window( handle ); }

- 1,510
- 19
- 17
Try this code, it is very easy to understand.
WebDriver driver = new FirefoxDriver();
driver.get("http://demo.guru99.com/popup.php");
driver.findElement(By.xpath("html/body/p/a")).click();
// return the parent window name as a String
String parentWindow=driver.getWindowHandle();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Pass a window handle to the other window
for(String childWindow: driver.getWindowHandles())
{
System.out.println("Switch to child window");
//switch to child window
driver.switchTo().window(childWindow);
//find an element and print text of it
WebElement textLabel=driver.findElement(By.xpath("html/body/div[1]/h2"));
System.out.println(" text: "+textLabel.getText());
driver.close();
}
System.out.println("Get back to parent window");
//switch to Parent window
driver.switchTo().window(parentWindow);
//find an element and print text of it
WebElement logotext=driver.findElement(By.xpath("html/body/div[1]/h2"));
System.out.println("text: "+logotext.getText());
driver.close();
}

- 4,607
- 2
- 15
- 36