Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

Get Window Handle Hangs. #3615

Closed
lukeis opened this issue Mar 3, 2016 · 9 comments
Closed

Get Window Handle Hangs. #3615

lukeis opened this issue Mar 3, 2016 · 9 comments

Comments

@lukeis
Copy link
Member

lukeis commented Mar 3, 2016

Originally reported on Google Code with ID 3615

What steps will reproduce the problem?
1.Unzip the Attached html folder.
2.In the Code Change the path to where you've unzipped the folder to Index.html
3.Run the code

What is the expected output? What do you see instead?
Get Window Handle Hangs.

Selenium version:2.17
OS:Windows 7Home Basic 64Bit
Browser:Firefox
Browser version:10.0.2

The Following is the code:

import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class AlertClass {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.get("file:///D:/html/Index.html");
        System.out.println("Before click");
        driver.findElement(By.xpath("html/body/table/tbody/tr/td[2]/a"))
                .click();
        // Thread.sleep(30000L);
        Set<String> windows = driver.getWindowHandles();
        Iterator<String> iter = windows.iterator();
        System.out.println(driver.getWindowHandles().size());

        // Set Parent windowhandle
        String parent = iter.next();

        // Switch to new window
        driver.switchTo().window(iter.next());
        // get label text in the new window
        System.out.println(driver.findElement(By.xpath("html/body")).getText());
        // close the new window
        driver.close();
        // switch to parent
        driver.switchTo().window(parent);
        // close main window
        driver.close();

    }
}

Reported by hemanth716 on 2012-03-24 09:47:25


- _Attachment: [html.zip](https://storage.googleapis.com/google-code-attachments/selenium/issue-3615/comment-0/html.zip)_
@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Hi Hemanth,

This is issue with Modal Dialog: window.showModalDialog()

Looks this issue is not fixed yet, i tried with 2.20 libraries but with no luck.

Would like to add up to this report:
I've a case similar quoted as below:

html:
========
<table>
   <tr>
     <td colspan="1">
          <a href="javascript:ShowProviderPopUp()">
          <img id="ctl00_Image1" src="../../../Images/Plus.png" align="middle" style="border-width:0px;"
/></a>
      </td>
          <td valign="middle">
           <a href="javascript:ShowProviderPopUp()">Register New Provider</a>
           </td>
    </tr>
</table>

<script language="javascript">
    var WinSettings = "center:yes;resizable:no;dialogHeight:550px;dialogWidth:1200px";
    function ShowProviderPopUp() {
        var url = "../../Super/CreateProvider.aspx";
        var retVal = window.showModalDialog(url, null, "status=0;help=0;maximize:no;minimize:no;center:yes;resizable:no;dialogHeight:600px;dialogWidth:900px;titlebar=no,toolbar=no,statusbar=no;");
    }
</script>

Java Source:

driver.findElement(By.id("ctl00_Image1")).click();
Set<String> windows = driver.getWindowHandles();
System.out.println(windows.size());

  for(String handler : windows){
     driver.switchTo().window(handler);
     if (driver.getCurrentUrl().contains("CreateProvider.aspx")){
      System.out.println("Get focus on Create Provider window");  
      break;  
      }  

  }
//Close modal window
 driver.close();
//Switch back to parent
driver.switchTo().window(parentWindowHandle);
//Close parent window 
driver.close();

Observed behavior:
Find some ambiguous results: Some times it runs smoothly, and gives back 2 window handles
and closes the modal and switch back to parent and closes it. ie. Code works :)

While most of the times, it hangs at getWindowHandles() and upon closing the modal
pop up, next lines of code are executed.

Wondering why sometimes the code works?

I learned that javascript executing stops once a modal popup launches and only after
closing focus comes back to parent. but using WebDriver i thought this would not be
an issue

Reported by kothapalli.sateeshbabu on 2012-03-24 11:01:26

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Reported by barancev on 2012-03-27 05:43:29

  • Labels added: Component-WebDriver, Browser-Firefox

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Hi Hemanth, 

I did a small change in code, it works fine. :)

Try the following code:

import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class AlertClass {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.get("file:///D:/html/Index.html");

        String parent = driver.getWindowHandle();

        driver.findElement(By.xpath("html/body/table/tbody/tr/td[2]/a")).click();

        Set<String> availableWindows = driver.getWindowHandles();
        String newWindow = null;
        for (String window : availableWindows) {
            if (!parent.equals(window)) {
                newWindow = window;
            }
        }
        // assertNotNull(newWindow);

        // Switch to new window
        driver.switchTo().window(newWindow);
        // get label text in the new window
        System.out.println(driver.findElement(By.xpath("html/body")).getText());
        // close the new window
        driver.close();
        // switch to parent
        driver.switchTo().window(parent);
        // close main window
        driver.close();
    }
}

Here after clicking on the link, new window gets open, so there are now two windows.

your code:
        Set<String> windows = driver.getWindowHandles();
        Iterator<String> iter = windows.iterator();
        System.out.println(driver.getWindowHandles().size());

        // Set Parent windowhandle
        String parent = iter.next();

        // Switch to new window
        driver.switchTo().window(iter.next());

But how do you assume first item is parent and the next is new window in the set of
windows ?

Hope it helps you :) cheers,,,

Reported by kopp.sam on 2012-03-28 13:45:09

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Hi Sam,

I've tried the above code.It doesn't work in my Environment 

Selenium version:2.17
OS:Windows 7Home Basic 64Bit
Browser:Firefox
Browser version:10.0.2

I've also tried Selenium version:2.20

Can you let me know your environment.

Reported by hemanth716 on 2012-03-28 14:46:29

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Hello kopp.sam@gmail.com,

I just tried to apply your solution, but it still not working for me.

I made my tests with Firefox 11, 10 and 9: I got the same result => the reference of
my dialog box is not find.

FYI: I'm running my tests on Mac OS Lion.

Thank you for your return.

Reported by franck.nouama on 2012-03-28 15:42:28

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Hi Hemanth and franck.nouama@gmail.com,

Following is my environment:
Selenium version: Working with jar files which I've downloaded in sep 2010, and I am
not sure of the version number.
OS: Windows Xp
Browser: Firefox 3.6.28

Here I am able to run the test and finally it is able to close the browser also.

Suggestion: After clicking on the link, just wait for 1 sec and try.

Reported by kopp.sam on 2012-03-29 05:34:18

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

I have run my tests with the same version of firefox, but I still have the error

Reported by franck.nouama on 2012-03-29 14:55:43

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Reported by barancev on 2012-05-19 20:48:49

@lukeis
Copy link
Member Author

lukeis commented Mar 3, 2016

Reported by luke.semerau on 2015-09-17 18:15:04

  • Labels added: Restrict-AddIssueComment-Commit

@lukeis lukeis closed this as completed Mar 3, 2016
@SeleniumHQ SeleniumHQ locked and limited conversation to collaborators Mar 4, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant