2016-07-06 57 views
0

我试图在两个打开的Popups之间切换。但driver.WindowHandles只返回1个句柄(ID)。我不知道如何切换到第二个弹出窗口。 命令driver.SwitchTo().ActiveElement不起作用。Selenium WindowHandles没有检测到所有打开的弹出窗口

ReadOnlyCollection<string> currentHandlesList = driver.WindowHandles; 
Console.WriteLine(currentHandlesList.Count); 

的结果如下:1

为什么它返回1.为什么不是2?

非常感谢。

回答

0

以下方法,你应该使用: -

string currentHandle = driver.CurrentWindowHandle; 
//Save the currently-focused window handle into a variable so that you can switch back to it later. 

ReadOnlyCollection<string> originalHandles = driver.WindowHandles; 
//Get the list of currently opened window handles. 

// Now work here to open popups 

// WebDriverWait.Until<T> waits until the delegate return the popup window handle. 
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5)); 

string popupWindowHandle = wait.Until<string>((d) => 
{ 
    string foundHandle = null; 

    // Subtract out the list of known handles. In the case of a single 
    // popup, the newHandles list will only have one value. 
    List<string> newHandles = driver.CurrentWindowHandles.Except(originalHandles).ToList(); 
    if (newHandles.Count > 0) 
    { 
     foundHandle = newHandles[0]; 
    } 

    return foundHandle; 
}); 

driver.SwitchTo().Window(popupWindowHandle); 

// Do whatever you need to on the popup browser, then... 
driver.Close(); 
driver.SwitchToWindow(currentHandle); 

这样你可以用弹出窗口工作..希望它会帮助你.. :)

相关问题