8

I have a circumstance in which clicking a link webpage opens a popup window. And after the popup window opens the focus is in the popup window and master window is disabled. And i am unable to get the control transferred to the popup window. Please have a look at the following code.

driver.findElement(By.linkText("Click me")).click();// when this line of code is reached then a popup window opens.

System.out.println("After Clicking me"); // After the popup window opens this line of code is never executed.

I am unable to transfer the control from parent window to popup window. I am aware of the following command.

driver.switchTo().window("popup window");

But its not helping much. please help me.

Prasad
  • 476
  • 5
  • 15
coolswastik
  • 439
  • 1
  • 7
  • 21

4 Answers4

9

This is a code i use when i need to work with a following pop-up window, close it and go back to my main window. Of course it has been simplified for the purpose of this answer. It maintains a handle of the original window (main) so it can make a difference between the others.

It requires an explicit WebDriverWait because i did have problems during development that code got run before the window actually got open, so this might not be a ideal condition,

function manipulatePopUp(final WebDriver driver, final WebDriverWait wait) {
    final String mainWindowHandle = driver.getWindowHandle();
    driver.findElement(By.id("linkThatOpensPopUp")).click();

    wait.until(new ExpectedConditions<Boolean>() {
        @Override
        public Boolean apply(WebDriver d) {
            return (d.getWindowHandles().size() != 1);
        }
    });

    for (String activeHandle : driver.getWindowHandles()) {
        if (!activeHandle.equals(mainWindowHandle)) {
            driver.switchTo().window(activeHandle);
        }
    }

    driver.close();
    driver.switchTo().window(mainWindowHandle);
}
Archimedes Trajano
  • 22,850
  • 10
  • 113
  • 154
aimbire
  • 3,637
  • 1
  • 13
  • 28
  • Can you please explain more about the wait.until method syntax and the logic in it? – Code Enthusiastic Feb 19 '13 at 06:33
  • 1
    By the way, driver.getWindowHandles() returns a Set not a List. – Code Enthusiastic Feb 19 '13 at 06:40
  • The wait basically keeps looping until the driver has more than one handle, by that means giving me the proper timing to be sure the pop-up is now ready to be interacted with. – aimbire Feb 19 '13 at 11:43
  • That's a comfortable way to be assured that once it gets on the for() it will loop until upon finding a window that it's not my main one. – aimbire Feb 19 '13 at 11:45
  • +1 Elegant IMO. Just a few typos: it should be new ExpectedCondition (singular). And above comment is correct, I used `for (Iterator it = driver.getWindowHandles().iterator(); it.hasNext(); ) { ... }`. – kcostilow Jun 25 '14 at 14:39
9

driver.findElement(By.linkText("Click me")).click();// when this line of code is reached then a popup window opens.

System.out.println("After Clicking me"); // After the popup window opens this line of code is never executed.

The line of code is never executed because the process is waiting for the popup to be handled.

getWindowHandles() works properly in this situation.

Example:

//handle of the master window before clicking the link
String master = driver.getWindowHandle();

driver.findElement(By.linkText("Click me")).click();

//logic for waiting for the popup, checking the size to become greater than 1 or breaking after sometime to avoid the infinite loop.
int timeCount = 1;

do
{
   driver.getWindowHandles();
   Thread.sleep(200);
   timeCount++;
   if ( timeCount > 50 ) 
   {
       break;
   }
}
while ( driver.getWindowHandles().size == 1 );

//Assigning the handles to a set
Set<String> handles = driver.getWindowHandles();
//Switching to the popup window.
for ( String handle : handles )
{
    if(!handle.equals(master))
    {
         driver.switchTo().window(handle);
    }
}

Now driver is switched to the popup window. If the popup window has a frame then you need to switch to the frame before identifying elements in it.

Code Enthusiastic
  • 2,687
  • 5
  • 21
  • 36
0
public class socialSignOn extends masterBaseClassNewSiteStage {

    @Test
    public void testSocialSignOn() throws Throwable {
        openParticularUrl("/my-lfc/join/user-details?user_type=free");

        driver.findElement(By.cssSelector("#socialSignOn > div.left.socialLogin.googleButton")).click();

        String MainWindow = driver.getWindowHandle();

        for (String activeHandle : driver.getWindowHandles()) {
            if (!activeHandle.equals(MainWindow)) {
                driver.switchTo().window(activeHandle);
            }
        }
        driver.findElement(By.cssSelector("#Email")).sendKeys("");
        driver.findElement(By.cssSelector("#next")).click();
        pauseFiveSeconds();
        driver.findElement(By.cssSelector("#Passwd")).sendKeys("");
        driver.findElement(By.cssSelector("#signIn")).click();
        pauseOneSecond();
        driver.switchTo().window(MainWindow);
        pauseTenSeconds();
        closeDriver();
    }
}
HDJEMAI
  • 7,766
  • 41
  • 60
  • 81
Danny
  • 277
  • 1
  • 12
-1
// delay : max number of seconds
new WebDriverWait(driver, delay * 1000).until(ExpectedConditions.alertIsPresent());
drive`enter code here`r.switchTo().alert().accept();
Sarah
  • 1
  • 1