Here is code to work with more than two windows, identify the windows based on name not by number.
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://irctc.co.in/");
Thread.sleep(400);
//click on Hotels @ Lounge
driver.findElement(By.xpath("//div[@id='bluemenu']/ul/li[6]/a")).click();
Thread.sleep(400);
driver.findElement(By.xpath("//div[@id='bluemenu']/ul/li[7]/a")).click();
Thread.sleep(400);
driver.findElement(By.xpath("//div[@id='bluemenu']/ul/li[8]/a")).click();
Thread.sleep(400);
Set<String> cWinHndls = driver.getWindowHandles();
for(String cWinHndl:cWinHndls){
String cWinHndlTitle = driver.switchTo().window(cWinHndl).getTitle();
System.out.println("Window Title: "+cWinHndlTitle);
if(cWinHndlTitle.equalsIgnoreCase("IRCTC Hotel India, Book Confirm Hotels, Retiring Room Indian Railways")){
driver.findElement(By.xpath("//section[@id='mainpannel']/div[2]/div[1]/div[1]/table/tbody/tr/td[2]/input")).sendKeys("2223334445");
}
if(cWinHndlTitle.equalsIgnoreCase("LTC Tour Packages By IRCTC, Confirm Ticket, Tourist Charters India")){
driver.findElement(By.xpath("//ul[@id='main-menu']/li[1]/a")).click();
}
if(cWinHndlTitle.equalsIgnoreCase("Heritage Monuments of India")){
driver.findElement(By.xpath("//div[@id='intro']/div[2]/div[2]/div/form/input")).click();
}
}
driver.switchTo().window(mWinHndl);
}
To know FAQ's on Selenium Please ClickHere
To know SQL's Queries Required For Testers Please ClickHere
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://irctc.co.in/");
Thread.sleep(400);
//click on Hotels @ Lounge
driver.findElement(By.xpath("//div[@id='bluemenu']/ul/li[6]/a")).click();
Thread.sleep(400);
driver.findElement(By.xpath("//div[@id='bluemenu']/ul/li[7]/a")).click();
Thread.sleep(400);
driver.findElement(By.xpath("//div[@id='bluemenu']/ul/li[8]/a")).click();
Thread.sleep(400);
Set<String> cWinHndls = driver.getWindowHandles();
for(String cWinHndl:cWinHndls){
String cWinHndlTitle = driver.switchTo().window(cWinHndl).getTitle();
System.out.println("Window Title: "+cWinHndlTitle);
if(cWinHndlTitle.equalsIgnoreCase("IRCTC Hotel India, Book Confirm Hotels, Retiring Room Indian Railways")){
driver.findElement(By.xpath("//section[@id='mainpannel']/div[2]/div[1]/div[1]/table/tbody/tr/td[2]/input")).sendKeys("2223334445");
}
if(cWinHndlTitle.equalsIgnoreCase("LTC Tour Packages By IRCTC, Confirm Ticket, Tourist Charters India")){
driver.findElement(By.xpath("//ul[@id='main-menu']/li[1]/a")).click();
}
if(cWinHndlTitle.equalsIgnoreCase("Heritage Monuments of India")){
driver.findElement(By.xpath("//div[@id='intro']/div[2]/div[2]/div/form/input")).click();
}
}
driver.switchTo().window(mWinHndl);
}
To know FAQ's on Selenium Please ClickHere
To know SQL's Queries Required For Testers Please ClickHere