2

Selenium WebDriver - 从子窗口切换回父窗口时挂起或卡住。如果我在调试模式下手动更改父窗口中的特定页面,则会成功从子窗口切换到父窗口。猜测父窗口中的特定页面会阻止窗口切换,因为它期望子窗口关闭。我该如何克服这个问题?(将控制权带回父窗口以进行进一步验证)(还建议是否有任何替代方法可用于切换视窗)

代码:(使用正确的代码)

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

}

4

1 回答 1

1

问题

根据您的代码试验,您正在执行首先打开子窗口的操作。接下来,您尝试将父窗口句柄存储为String parentWin = browser.getWindowHandle();. 但是到那时子窗口已启动,因此子窗口句柄存储在parentWin. 因此 WebDriver 以后无法切换到真正的父窗口。

解决方案

在执行打开子窗口的操作之前,将父窗口的窗口句柄存储在字符串中。这是工作代码集:

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 );
  }
}
于 2017-06-27T14:02:09.490 回答