2

Selenium WebDriver- hungs or stucks while switching back from child window to parent window. If I change the specific page in parent window Manually on debug mode,Successfully switch is happening from child to parent window. Guessing that specific page in parent window blocks switching of windows as it expects child window to be closed.How can i overcome this Issue?(To bring back control to parent window for further validation)(Also Suggest if any alternative methods are available to switch windows)

Code:(Used Right Code)

String parentWin = browser.getWindowHandle();
Set<String> handles = browser.getWindowHandles();
String winHandle = null;
Iterator<String> itr = handles.iterator();while(itr.hasNext())
{
    winHandle = itr.next();
    if (!winHandle.equals(parentWin)) {
        browser.switchTo().window(winHandle); //Tried Giving Enough delay also
        browser.switchTo().window(parentWin);// It hungs here (Executes at
                                                // the case if change the
                                                // specific page in parent 
                                                // window)
    }

}

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
karthick
  • 21
  • 3

1 Answers1

1

The Issue

As per your code trials, you are performing the action to open the child window first. Next you are trying to store the parent window handle as String parentWin = browser.getWindowHandle();. But by that time the child window is initiated so the child window handle gets stored in parentWin. Hence WebDriver is unable to switch to the real parent window later.

The Solution

Before you perform the action for opening the child window store the window handle of the parent window in a String. Here is the working set of code:

String parentWin = browser.getWindowHandle();
//perform the action/click which opens a child window
//Now create the Set
Set<String> handles = browser.getWindowHandles();
//Create iterator to traverse
Iterator<String> itr = handles.iterator();
//create a while loop if there are multiple window handles
while(i1.hasNext())
{
  //Store the Child window handle
  String child_window = i1.next();
  //Check if parent window handle not equals child window handle
  if (!parentWin.equalsIgnoreCase(child_window))
  {
    //child window present, so switch to child
    driver.switchTo().window(child_window);
    //Do your work here on child window
    //switch back to parent window
    browser.switchTo().window(parentWin );
  }
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I also performed same operation as you mentioned in the comment. Please refer my code. Thanks – karthick Jun 27 '17 at 14:10
  • @karthick Check my updated Answer with updated comments. Hope that helps you. Thanks – undetected Selenium Jun 27 '17 at 14:28
  • Hi debanjan, I have performed same as you said. Actually, it works switching. But in specific page of parent window Driver.switchto.window(ParentWinInst) gets hung. When i try to changing page in parent win manually at debug mode,Control passes to parent window. But my question is why cant I switch to parent window while i have that specific page. Also verified that window handle had two different session Ids. Thanks – karthick Jun 27 '17 at 15:45
  • @karthick Can't comment more without seeing your actual code. But definately the code you pasted there was an error which I have elaborated. Thanks – undetected Selenium Jun 27 '17 at 15:50
  • I also have tried the above solution. But i did not help my problem. Thanks – karthick Jun 28 '17 at 05:57