Most of the time when on any action, a new window is opened then the control remains on the first window but the element is on the second window, so we receives an error message that "element not found". In this case we have to move the control explicitly on the second window.
Use the following steps:
1. Copy all the window ids in a set.
2. Iterate through the windows using next() method of iterator class.
Lets take an example of HDFC bank site.
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.hdfcbank.com/");
driver.manage().timeouts().implicitlyWait(20L, TimeUnit.SECONDS);
System.out.println("--------Generating window ids from first window------");
Set winids = driver.getWindowHandles();
Iterator itrate = winids.iterator();
String first_window = itrate.next(); // window id of first window
System.out.println(first_window);
if(driver.findElement(By.xpath("//*[@id='c1322826231823']/div[2]/div[2]")).isDisplayed()){
driver.findElement(By.xpath("//*[@id='c1322826231823']/div[2]/div[2]/a/img")).click();
}
driver.findElement(By.xpath("//*[@id='netbanking']")).click();
driver.findElement(By.xpath("//*[@id='loginsubmit']")).click();
System.out.println("---------Generating window ids from second window-------");
winids = driver.getWindowHandles();
itrate = winids.iterator();
System.out.println(itrate.next()); // window id of first window
String second_window = itrate.next(); // window id of second window
System.out.println(second_window);
driver.switchTo().window(second_window);
driver.findElement(By.xpath("//*[@id='wrapper']/div[7]/div[2]/div[1]/p/a/strong")).click();
System.out.println("---------Generating window ids from third window-------");
winids = driver.getWindowHandles();
itrate = winids.iterator();
System.out.println(itrate.next()); // window id of first window
System.out.println(itrate.next()); // window id of second window
String third_window = itrate.next(); // 3rd window
System.out.println(third_window);
driver.switchTo().window(third_window);
System.out.println(driver.findElement(By.xpath("html/body/div[1]/ul/li[1]/strong")).getText());
0 Comment(s)