selenium WebDriver- hungs or stucks while switching from child window to parent window (IE11 and Win 8.1)

一曲冷凌霜 提交于 2019-12-11 00:18:42

问题


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)
    }

}


回答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 );
  }
}


来源:https://stackoverflow.com/questions/44780691/selenium-webdriver-hungs-or-stucks-while-switching-from-child-window-to-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!