I am using below code to test window handle.
New page opens in new tab but switchTo
command do not switch to it.
public static void main(String[] args) {
Actions act = new Actions(driver);
driver.get("https://www.google.co.in");
String parentWin = driver.getWindowHandle();
act.keyDown(Keys.LEFT_CONTROL).perform();
driver.findElement(By.linkText("Images")).click();
for(String newWindow: driver.getWindowHandles()){
driver.switchTo().window(newWindow);
driver.findElement(By.id("lst-ib")).sendKeys("hello world");
driver.findElement(By.name("btnG")).click();
driver.close();
}
driver.switchTo().window(parentWin);
driver.findElement(By.linkText("Gmail")).click();
}
This code voted as correct answer here: How to handle the new window in Selenium WebDriver using Java? from @CODEBLACK