我在幻灯片放映中单击了一个图像,它会生成一个新窗口。窗口打开后,我需要将注意力集中在新窗口上并做一些事情。我在下面附上了我的代码,但它不成功。
@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;
}