5

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.

Community
  • 1
  • 1
Code Enthusiastic
  • 2,827
  • 5
  • 25
  • 39
  • Is this GWT or Jquery based window? – Chandana Dec 31 '12 at 07:20
  • It is javascript. javascript:LSellAccomDropDown();Reset('STC', false); – Code Enthusiastic Dec 31 '12 at 07:31
  • if it is JavaScript based dialog window, you many no need to move to Modal Window. Most of the time those kind of windows also in same source code level. `Check following steps: press button, wait 2-3 seconds, set values or do operation in window(without moving to window)` – Chandana Dec 31 '12 at 07:36
  • @Chandana That isn't working. Driver can't able to identify the elements. It seems Webdriver focus is still on parent window, may be we've to switch the driver to modal window before identifying elements in it. – Code Enthusiastic Dec 31 '12 at 07:55
  • Without looking at page level information it's very difficult to help on this kind of issues. :( – Chandana Dec 31 '12 at 07:58
  • @Chandana I would've already posted the link to the webpage in the question if the webpage was intended for the public. – Code Enthusiastic Dec 31 '12 at 08:01
  • Please check the following link for solution. Hope it helps ! http://stackoverflow.com/questions/13936167/how-to-deal-with-modaldialog-using-selenium-webdriver – HemChe Dec 31 '12 at 12:50
  • It would be a lot easier to help if we can reproduce your problem. Can you share a picture of your modal dialog and the html that creates the window? Is it the modal dialog seen at http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog2.htm? – Justin Ko Jan 11 '13 at 03:38
  • Yeah, it is similar to the one you mentioned in your comment. – Code Enthusiastic Jan 11 '13 at 06:02
  • Does printing driver.getWindowHandles() show more than one window handle? – user650309 Jan 14 '13 at 12:26

5 Answers5

5

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.

Community
  • 1
  • 1
Prashant Shukla
  • 1,391
  • 9
  • 18
  • This pop does have iframe tag and html, head, body tags and a title tag too. This is what makes me think that it is a different window. – Code Enthusiastic Jan 10 '13 at 11:36
  • I thought so. Having a iframe tag (and html, head, body tags under it) makes it kindda of a sub-page within main page. Try my switchTo().frame() method and i am sure it will work. I am going to update a reference link for you in the answer. – Prashant Shukla Jan 10 '13 at 11:40
  • See the order of tags in the hierarchy is. html, head(title under head), body(iframe under body) then html, head, body under this iframe tag. – Code Enthusiastic Jan 10 '13 at 11:42
  • Thats right .. thats how a frame is defined. Try the link I have updated in the answer.... it will work .. :) – Prashant Shukla Jan 10 '13 at 11:53
  • WebDriver is throwing NoSuchFrameException. – Code Enthusiastic Jan 10 '13 at 12:06
  • There might be frame within frame (ie frame tag inside another frame tag). Start from top frame and go down till the frame just above the Modal Window. Or if you could point me to the website / html I might be able to do it for you. – Prashant Shukla Jan 10 '13 at 12:09
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22517/discussion-between-prashant-shukla-and-code-enthusiastic) – Prashant Shukla Jan 10 '13 at 12:11
  • I've seen the whole html source of the main window and there was only one frame "frame". I've also seen the html source of the modal window and it has only one frame "iframe". I am not able to find the html source of modal window in the html source of main window makes me think these two are completely different windows? – Code Enthusiastic Jan 10 '13 at 13:48
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);
    }
}
Code Enthusiastic
  • 2,827
  • 5
  • 25
  • 39
1

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.

0

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

Community
  • 1
  • 1
Matthew Petty
  • 652
  • 5
  • 7
0

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.

Abhishek_Mishra
  • 4,551
  • 4
  • 25
  • 38