Unable to get childwindow title and url

1,198 views
Skip to first unread message

testingzeal

unread,
Nov 15, 2014, 2:35:56 PM11/15/14
to webd...@googlegroups.com
I am having issues in getting title,url of a child window and test always print Parent Window tile,url. I searched for this issue and appears that i am using the same logic what other's are using.

Using 2.39 WebDriver
IE 11.0

Below code works mostly fails to return child window title and url , but it worked few times.

Here the code i am using -


Clicks a link on parent window which opens a child window

String ParentWindow = driver.getWindowHandle();
            Thread.sleep(200);
    for (String childHandle: driver.getWindowHandles()) {
                System.out.println("Entering for loop of window handles");
            Thread.sleep(100);
                driver.switchTo().window(childHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
                Thread.sleep(100);
                driver.manage().window().maximize();
                Thread.sleep(200);
                System.out.println("Window Title : " + driver.switchTo().window(childHandle).getTitle());
                System.out.println("Window Url : " + driver.switchTo().window(childHandle).getCurrentUrl());



                Thread.sleep(200);
               abc.searchByZipCode(zipCode);
}

Krishnan Mahadevan

unread,
Nov 15, 2014, 8:14:39 PM11/15/14
to webdriver

Without you telling us how does the html look like, it's almost close to impossible to guess what is going on.

It might not even be a child window but just a div or a span which perhaps looks like a window which explains why it doesn't work.

Or it might be a timing issue.

So please provide a public url wherein this problem can be simulated or a full html page using which the problem can be simulated.

Please don't forget to include a standalone test which can be executed to recreate the problem.

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.

darrell

unread,
Nov 16, 2014, 11:07:31 AM11/16/14
to webd...@googlegroups.com
This isn't a horrible example piece of code but it is missing the output. It does eliminate the idea that the child window is really a div in the same DOM as the parent window. The fact that it 'worked a few times' indicates that a child window does exist but the majority of the time your code is not detecting the child window.

This hints at a timing issue. Whenever I see intermittent behavior I suspect timing is the issue. The easiest way to test this theory is adding large sleep statements. I created two html pages. The first page is called 'index.html' and it contains a link which opens a new window called 'child.html'. The index.html title is 'index' and the child.html title is 'child'. If I use:

        driver.get("file:///Users/darrellgrainger/index.html");
        driver.findElement(By.cssSelector("#link2")).click();
        Set<String> handles = driver.getWindowHandles();
        for(String handle : handles) {
            driver.switchTo().window(handle);
            System.out.print(handle + ": ");
            System.out.println(driver.getTitle());
        }

It opens index.html, clicks the link then iterates over the window handles. This seems to work well when dealing with static html pages. If your html is using AJAX and is very javascript heavy then it is possible that the title of the page isn't available the moment the page is loaded. This means you need to wait for the title to exist. It could even mean you have to wait for the child window to exist. For example, I click the link which opens the child window. It then runs a LOT of javascript or conducts AJAX calls. Only after the javascript finishes then it opens the child window. If your Selenium code is attempting to find the child window and the web page is still processing javascript then the child window won't exist and your code will fail.

The way to test this is:

        driver.get("file:///Users/darrellgrainger/index.html");
        driver.findElement(By.cssSelector("#link2")).click();
        Thread.sleep(10000);
        Set<String> handles = driver.getWindowHandles();
        for(String handle : handles) {
            driver.switchTo().window(handle);
            System.out.print(handle + ": ");
            System.out.println(driver.getTitle());
        }

In this example I click the link to make the child window appear but then I sleep for 10 seconds before checking for the child window. If this works 100% of the time then the theory is correct. We have to wait for something to complete before the child window appears. If we know a child window will be created then we could do something like:

        int n = driver.getWindowHandles().size();
        int attempts = 0;
        // do the action which causes a child window to appear
        while ((driver.getWindowHandles().size() == n) && attempts < 30) {
                Thread.sleep(100);
                 attempts++;
        }

If we are waiting 100ms between checks for 30 iterations then this will wait 3 seconds before it gives up. You can play with the numbers to wait longer or shorter as you see fit. However, when this loop finishes if the attempts reach the maximum or Selenium finds the child window. So if attempts == 30 is false we could assume the child window is present and we can switch to it. Now this all assumes that my theory is correct. If it is something else we'll have to come up with another theory and test that theory.

testingzeal

unread,
Nov 17, 2014, 4:22:04 PM11/17/14
to webd...@googlegroups.com
Thanks a ton, Darell. Your details are awesome.

Mine was surely a timing issue, I was using sleep at wrong place, after your details i added wait at the right place and it seems to be working.

One question though -

i expect below logic to print

Parent window title and then child window title

But intermittently it is printing child window title first and then parent window title at the end and that is causing my test to fail


 
    for(String handle : handles) {
            driver.switchTo().window(handle);
            System.out.print(handle + ": ");
            System.out.println(driver.getTitle());
        }



=============Here is my logic in the test


String parentHandle = driver.getWindowHandle();
driver.findElement(By.linkText("Request")).click(); // clicking on this link opens child window
cti.waitForChildWindow();
 cti.handleMultipleWindows();

=====method definitions

//Wait for child window
    public void waitForChildWindow() throws Exception
    {
        driver.switchTo().defaultContent();
        String errorMsg="Unable to Open child window";
        try
        {
            int n = driver.getWindowHandles().size();
            System.out.println("Window size is :"+n);
            if(n==2)
            {

                int attempts = 0;
                // do the action which causes a child window to appear
                while ((driver.getWindowHandles().size() == n) && attempts < 300) {
                    Thread.sleep(200);
                    attempts++;
                }
            }
            else
            {
                throw new Exception(errorMsg);
            }
        }
        catch(Exception e)
        {
            throw new Exception("Unable to open  child Window");
        }
    }



    //Switch to child window when there are multiple windows
    public void handleMultipleWindows() throws Exception{

        try
        {

            Set<String> handles = driver.getWindowHandles();
            for (String handle : handles) {

                driver.switchTo().window(handle);
                System.out.print(handle + ": ");
                System.out.println(driver.getTitle());
                driver.manage().window().maximize();
            }
        }
        catch(Exception e)
        {
            throw new Exception("Unable to switch to child window");
Reply all
Reply to author
Forward
Message has been deleted
0 new messages