2016-07-06 2 views
0

Я пытаюсь переключаться между двумя открытыми всплывающими окнами. Но 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); 

Таким образом, вы можете работать с всплывающими окнами .. надеемся, что это поможет вам .. :)