Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to select the current window,frame or popup where my browser is pointing. (i.e when i doubleclick or click it opens an popup or window) I want to get through that window or popup using selenium webdriver.So if possible please suggest me how to do it. Thanks in advance.

share|improve this question
up vote 1 down vote accepted

To switch to new pop up window what you can do is the following:

String currentWindow = driver.getWindowHandle();
String lastWindow = null;
 Set<String> handles = driver.getWindowHandles();
    for (String aux : handles) {
        lastWindow = aux;

This will switch you to the new window opened. If you need to switch to the other window just switch to currentWindow.

Hope this helps.

share|improve this answer
sure i will work on this. – Emmanuel Angelo.R Feb 28 '14 at 4:44

You can use the following code also to handle popup.

 String mainwindow = driver.getWindowHandle();
        for (String popup : driver.getWindowHandles()){
// Your code on poppup window
        driver.switchTo().window(mainwindow); //Switching to main/parent window

Hope this will help :)

share|improve this answer
sure i will try it QA4it – Emmanuel Angelo.R Mar 4 '14 at 14:51

As an alternative you can try this,

Set <String>handles = driver.getWindowHandles();//To handle multiple windows
firstWinHandle = driver.getWindowHandle(); //To get your main window
String winHandle=handles.iterator().next();  //To find popup window
if (winHandle!=firstWinHandle){

driver.switchTo().window(secondWinHandle); //To switch to popup window

Make sure to add some delay after switching to next popup, Thread.sleep(5000);

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.