当前位置: 首页 > 知识库问答 >
问题:

Selenium WebDriver在Win 8.1中使用IE11从子窗口切换到父窗口时阻塞或卡住

康锦
2023-03-14

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

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个答案

江新
2023-03-14

根据您的代码测试,您将执行首先打开子窗口的操作。接下来,您将尝试将父窗口句柄存储为字符串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 );
  }
}
 类似资料: