I have clicked an image in a slide show and it generates a new window. Once the window opens I need to bring focus on new window and do some stuff. I have attached my code below, but it is not successful.
@Test
public void testCreateAccount() throws Exception{
//OPEN manorama online url
driver.navigate().to("http://www.manoramaonline.com/cgi-bin/MMOnline.dll/portal/ep/home.do?tabId=0&BV_ID=@@@");
// to click a link inside a slide show
while (!driver
.findElement(
By.xpath("/html/body/div[2]/div[2]/div[4]/div/div/div/div/center/div/div[2]/span/div/a/img"))
.isDisplayed()) {
}
driver.findElement(
By.xpath("/html/body/div[2]/div[2]/div[4]/div/div/div/div/center/div/div[2]/span/div/a/img"))
.click();
String newwindowTitle = driver.getTitle();
switchWindow(newwindowTitle);
}
protected static boolean switchWindow(String title) throws IOException {
String currentWindow = driver.getWindowHandle();
Set<String> availableWindows = driver.getWindowHandles();
if (!availableWindows.isEmpty()) {
for (String windowId : availableWindows) {
if (driver.switchTo().window(windowId).getTitle().equals(title)) {
return true;
} else {
driver.switchTo().window(currentWindow);
}}}
return false;
}