2016-07-06 85 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); 

這樣你可以用彈出窗口工作..希望它會幫助你.. :)

相關問題