'Switching control to a Modal window (not an alert) using Selenium Webdriver

I have a web application that launches the Save Window (which is Modal) when I click on the Save Button. This window takes uptil 10 seconds to load completely (I can counter this with a wait). I need to carry out some actions in this window, before I complete the save.

The problem I face is - The moment the modal window is launched, there is no way I can use a driver.SwitchTo() or driver.Manage().GetAllWindowHandles() etc. I confirmed this with the following lines of Code.

driver.findElement(By.xpath("//*[@id='toolbar']/a[1]")).click();
// After the above line is executed, the Popup gets launched

Set<String> sWindowHandles = driver.getWindowHandles();
System.out.println("Popup"); 
System.out.println(driver.getWindowHandles().size()); // This always prints "1"

The 3 lines above are not executed at all (or at least not for a long time) until I explicitly close the Popup Window.

How do I work on some control that exists within the Save Window (Modal), when there is no way I can find the Window's handler?

for(String winHandle : driver.getWindowHandles()){
    driver.switchTo().window(winHandle);
}

doesn't work, because immediately after the modal window is opened (makes sense to GetWindowHandles only after it is launched), the subsequent lines aren't executed at all. I'm caught in a deadlock. Please help me out.



Solution 1:[1]

If Windows handles is not working on an Application.
Java robot class function can be used.

      Robot robot = new Robot();   
//Doing a mouse over for the X and Y coordinates of button/link which opens modal window
robot.mouseMove(210,350);  
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);  
robot.delay(100);  
//Clicking tab til the cursor is on specific position (textbox/button)
robot.keyPress(KeyEvent.VK_TAB);  
robot.delay(100);  
//Doing a mouse over for the X and Y coordinates of button/link
robot.mouseMove(300,150);  
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);  
robot.delay(100);

For more information refer the below link. http://alvinalexander.com/java/java-robot-class-example-mouse-keystroke

Solution 2:[2]

I sometimes find the windowhandles can take a while to update with the correct value even though the popup is visible. To counter this I use a loop which breaks when the windowhandles reaches the expected size.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Naaz
Solution 2 CynicalBiker