Showing posts with label How to close a particular window. Show all posts
Showing posts with label How to close a particular window. Show all posts

Sunday, June 8, 2014

Suppose there are five Browser windows opened, how to find the third one and close it through web driver ?

1st get the address of all the browsers using

Iterator<String> windows = driver.getWndowHandles().iterator();

Now suppose you want to close the window whose title is "abc";

while(windows.next()){
         driver.switchTo().window(windows.next());
         String title = driver.getTitle();
         if(title.equals("abc")){
                driver.close();
                 break;
          }
}

or say if you want to close 3rd one by force and you don't know the expected title then

for(int i=0; i<3; i++){
         driver.switchTo().window(windows.next());
         if(i==2){
                driver.close();
                 break;
         }
}


or you can do the same -


for(int i=0; i<3; i++){
          String child = windows.next();
         if(i==2){
                 driver.switchTo().window(child);
                driver.close();
                 break;
         }
}