close a window

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

close a window

srinithya
when we  open  a link  for ex:- google.co.in   in that if we click on anyother link for ex:- gmail and if it redirects to another page  in  a new window.how can we close that  window ?
i tried driver.switch.window().close();
but its not working
is there any other way to close the window which is opened in new tab?
Reply | Threaded
Open this post in threaded view
|

Re: close a window

softwaretestingforum
Administrator
You need to pass window handle in switch.window("window handle")
and then you would be able to close it
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: close a window

mfsi_sitamj
In reply to this post by srinithya
Hi Srinithya,

You can use the following code for your purpose :-

                String currentWindowHandle=driver.getWindowHandle();
                String dialogWindowHandle=null;
                for(String windowHandle:driver.getWindowHandles())
                {
                        if (!windowHandle.equals(currentWindowHandle))
                    dialogWindowHandle=windowHandle;  
                }
                driver.switchTo().window(dialogWindowHandle);
                driver.close();

Thanks and regards
Sitam