Handling modal window in IE8 using selenium webdriver

199 views
Skip to first unread message

avanth kumar

unread,
Aug 12, 2014, 2:06:04 PM8/12/14
to seleniu...@googlegroups.com
Hi,

 In my application, there is a modal window on which i cannot do anything. scenario is i have a parent window opened. On the parent window i click a button, a new child window is opened. So i have 2 windows open. On the new child window i click a button and the child window closes and a new modal window is opened. So now i have only two windows, one is parent window and the other is the modal window.
   I can switch to the parent window only when the modal window is closed. Problem is i am not able to switch to the modal window and close it. But the focus is on parent window and the modal window is popped up. The only solution is to close the modal window. I have tried it in different ways still not able to get the focus or close the window.

Please let me know the solution on this, i just need to close the window. Please find the below code which i am using.


        window3 = driver.getWindowHandle();
        driver.switchTo().window(window3);
        System.out.println(driver.getTitle());
        windowList = driver.getWindowHandles();
        System.out.println("Number of Windows-->"
                + driver.getWindowHandles().size());
            iterator = windowList.iterator();
            String window4 =null;
        while(iterator.hasNext())
        {
            window4 = (String)iterator.next();
            System.out.println("Title : " + driver.getTitle());
        }
       
            driver.switchTo().window(window4);
            System.out.println("Title of the page after - switchingTo: " + driver.getTitle());
            System.out.println("Current url" + driver.getCurrentUrl());
           

        //driver.switchTo().activeElement();
        Actions actions=new Actions(driver);
        actions.keyDown(Keys.ALT);
        actions.perform();
        Keyboard keyboard = ((HasInputDevices) driver).getKeyboard();
        keyboard.pressKey(Keys.F4);

Chris Merrill

unread,
Aug 12, 2014, 5:02:29 PM8/12/14
to seleniu...@googlegroups.com
You didn't show what your output is...which leaves me wondering at which
point it is not working correctly. Does the window not appear in the
window list?

Also, that key press code looks a little funny to me:

On 8/12/2014 2:06 PM, avanth kumar wrote:
> //driver.switchTo().activeElement();
> Actions actions=new Actions(driver);
> actions.keyDown(Keys.ALT);
> actions.perform();
> Keyboard keyboard = ((HasInputDevices) driver).getKeyboard();
> keyboard.pressKey(Keys.F4);

Have you tried something like this?
Actions actions = new Actions(execution_context.getDriver());
actions.keyDown(Keys.ALT).keyDown(Keys.F4).keyUp(Keys.F4).keyUp(Keys.ALT).perform();




--
------------------------------------------------------------------------ -
Chris Merrill | Web Performance, Inc.
ch...@webperformance.com | http://webperformance.com
919-433-1762 | 919-845-7601

Web Performance: Website Load Testing Software & Services
------------------------------------------------------------------------ -

Smitarani Bhol

unread,
Aug 13, 2014, 2:39:52 AM8/13/14
to seleniu...@googlegroups.com
Hello Avanth,

I too faced the same modal window, a few days back. I have used below approach :

- First switch handle to parent window, then close modal window using javascript executor. Code snippet is as below:

                String parentWindow = driver.getWindowHandle();
String currentWindowURL = driver.getCurrentUrl();
Set<String> windowId = driver.getWindowHandles();
for (String window : windowId) {
driver.switchTo().window(window);
if (!driver.getCurrentUrl().equalsIgnoreCase(currentWindowURL)) {
break;
}
}
               // Do your modal window - actions- verifications
                JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("window.close();"); // It will close modal window - but selenium still has the handle
driver.switchTo().window(parentWindow);       // selenium gets parent window handle back

I hope this will help.

Thanks,
Smita

avanth kumar

unread,
Aug 13, 2014, 3:43:50 PM8/13/14
to seleniu...@googlegroups.com
Hi Smitarani,

 Thanks for the update. But the issue is i am not able to get the focus on the window to proceed. Actually getWindowHandles doesnt return the modal window which i am getting. Is there any solution on this? Appreciate your response. Thanks

avanth kumar

unread,
Aug 16, 2014, 2:52:16 AM8/16/14
to seleniu...@googlegroups.com
Hi Chris,
 
          Yes i tried the above code. Keysdown and keysup is only applicable for modifier keys like ALT, CRTL hence the above code doesnt work. Is there any way where i can press alt+F4 in webdriver. Thanks

Serguei Kouzmine

unread,
Aug 18, 2014, 6:34:16 PM8/18/14
to seleniu...@googlegroups.com

Hello Avanth

I assume you are running Selenium from Windows host. In This case you can use Powershell or C# format of test and

use the code that I posted on codeproject http://www.codeproject.com/Articles/799161/Dealing-with-Powershell-Inputs-via-Basic-Windows-F
section "Selenium Test"  and "Selenium IDE Powershell Formatter" to locate and close the dialog in question or  any Windows window in general by title fragmend and text fragment (e.g. child button text)
The example shown in the article targets save as dialog, but it is clear how to modify it for your particular mime type.
The window is found :

Matches button [459550] text : "&Save"
Window process ID is 1104
Window handle is 983844
Window title is Save As
Window match 1
Let me know if you need any help with that.

The  Powershell formatter Selenium IDE plugin is going to be submitted to mozilla dev very soon,

Thanks,

Serguei Kouzmine
Let me know


On Thursday, January 17, 2013 7:55:40 AM UTC-5, Debasish Dutta wrote:

    Hi All,

    I have attached screenshot of the dialogue box. I want to click on the ok/cancel button on the windows dialouge box. there is no xpath of the buttons. by click on the ok button excel file should be opened.How i can overcome this scenario??




    Thanks & Regards,

    Debasish Dutta
99.png
100.png

Serguei Kouzmine

unread,
Aug 19, 2014, 11:20:59 PM8/19/14
to seleniu...@googlegroups.com
Hello Avanth

Please reattach the dialog screen shot and if possible a mocup page that would open one. I will be able to tailor the example posted earlier to deal with your situation.

Thanks,

Serguei Kouzmine


On Tuesday, August 12, 2014 2:06:04 PM UTC-4, avanth kumar wrote:

Serguei Kouzmine

unread,
Aug 23, 2014, 6:58:23 PM8/23/14
to seleniu...@googlegroups.com
Hello Avanth,

Do you still need the code ?

I added the ability to put the text into save textbox and click save as button. However only the second has the correct effect. Explorer ignores the text I enter and attempts to save the file in the original location  / name .

Let me know if this will work for you.


Thanks,

Serguei Kouzmine

On Tuesday, August 12, 2014 2:06:04 PM UTC-4, avanth kumar wrote:

Serguei Kouzmine

unread,
Aug 24, 2014, 4:05:51 PM8/24/14
to seleniu...@googlegroups.com
Hello Avanth,

I fixed the code to make the Explorer accept the name of the file. Please visit the latest community approved version of the article
http://www.codeproject.com/Articles/799161/Dealing-with-Powershell-Inputs-via-Basic-Windows-F for the code. The code is in the zip attached to the article.
141.png
144.png
Reply all
Reply to author
Forward
0 new messages