How to switch from main window to popup window?

2019-02-22 13:17发布

问题:

I'm not talking about the popups like alert, confirm or prompt dialogs. In the application if I click on a button popup gets opened. I am not able to switch the WebDriver to the popup window.

I've tried to use getWindowHandles() but it only returns the main window handle.

I also tried switchTo.window("windowname") but it didn't work.

回答1:

Usually Modular Windows are part of the same DOM, unlike javascripts alerts. Only thing that sets them apart from rest of the page is that they are in different frame.

Try to see if this Modular Window lies inside a frame or iframe tag. If any of the parent is frame or iframe then you will have to change the context to that frame before you can performa any action on the Modal Window.

So find the frame do a driver.switchTo().frame() and then perform the action on the element you want to. Once the action is done, which would most probably bring you back to the main page. Use driver.switchTo().defaultContent() to bring focus back to main page.

This SO question will be helpful.

If this does not work it would be helpful to have a look at the page or its HTML.



回答2:

Are you using pageobjects?

If you are using this, you will need to find the elements after the popup appears, because initElements will not initialize them if they are not visible when you first open the page.



回答3:

//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);
    }
}


回答4:

Assuming your talking about a javascript alert.

final Alert a = driver.switchTo().alert(); 
a.accept(); 

or

Execute JavaScript directly to handle the altert

and

Maybe wait for the alert to show up



回答5:

As per webdriver this issue is fixed in 2.16 but still it does not work Support for window.ShowmodalDialog

You can use Java Robot class for handling such cases.

Example :

Wait(5000); // Wait for model pop, 
    int keyInput[] =
    {
      KeyEvent.VK_S, KeyEvent.VK_E, KeyEvent.VK_L, KeyEvent.VK_E,
      KeyEvent.VK_N, KeyEvent.VK_I, KeyEvent.VK_U, KeyEvent.VK_M,
    };   

    Robot robot = new Robot();

    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyPress(KeyEvent.VK_TAB);


    for (int i = 0; i < keyInput.length; i++)
    {    
      robot.keyPress(keyInput[i]);
      robot.delay(100);    
    }  

    robot.delay(1000); 
    robot.keyPress(KeyEvent.VK_TAB);
    robot.delay(1000);
    robot.keyPress(KeyEvent.VK_TAB);
    robot.delay(1000);
    robot.keyPress(KeyEvent.VK_TAB);

    robot.delay(1000);
    robot.keyPress(KeyEvent.VK_ENTER); // Save Btn 

The delay between events is necessary otherwise you will miss the events.