To switch latest window opened use below code:
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
Sampe code:
package test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SwithcToNewWindow {
public static void main(String[] arg) {
// Instantiate the FirefoxDriver
WebDriver driver = new FirefoxDriver();
// Visit the Selenium-WebDriver FAQ site
driver.get("http://seleniumwebdriverfaq.tumblr.com/");
// Print the title of the page - It should print
// "Selenium-WebDriver FAQ's"
System.out.println("Title of the page before - switchingTo: " + driver.getTitle());
//Now click on RSS button; it will open in new window
driver.findElement(By.xpath("//a[text()='RSS']")).click();
//Switch to newly opened window -RSS and get the page titele
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
System.out.println("Title of the page after - switchingTo: " + driver.getTitle());
// Close the browser window
driver.close();
// Quit the driver
driver.quit();
}
}