5

在测试过程中会弹出 2 个窗口。

我的代码:

string BaseWindow = driver.CurrentWindowHandle;                 
ReadOnlyCollection<string> handles = driver.WindowHandles;

foreach(string handle in handles)                    
{                         
    Boolean a = driver.SwitchTo().Window(handle).Url.Contains("Main");
    if (a == true)  
    {       
        InitialSetting.driver.SwitchTo().Window(handle);      
        break;
    }  
}                

我想切换到 url 包含“Main”的窗口。但是当测试运行时,它会在两个窗口之间不断切换,并且不会停止。

我调试并发现foreach即使boolean a是真的也没有中断。

我该如何解决这个问题?

4

5 回答 5

13
//switch to new window 
driver.FindElement(By.Id("link")).Click(); 

//wait for new window to open 
Thread.Sleep(2000); 

//get the current window handles 
string popupHandle = string.Empty; 
ReadOnlyCollection<string> windowHandles = driver.WindowHandles;  

foreach (string handle in windowHandles)  
{  
    if (handle != existingWindowHandle)  
    {  
         popupHandle = handle; break;  
    }  
}  

 //switch to new window 
driver.SwitchTo().Window(popupHandle); 

//check for element on new page 
webElement = driver.FindElement(By.Id("four04msg")); 
if(webElement.Text == "THE CONTENT YOU REQUESTED COULDN’T BE FOUND...")  
{  
    return false;  
}  
else  
{  
    return true;  
}  

 //close the new window to navigate to the previous one
driver.close(); 

//switch back to original window 
driver.SwitchTo().Window(existingWindowHandle);
于 2012-07-16T09:29:30.817 回答
3
  1. 使用原始邮政编码。

    string existingWindowHandle = driver.CurrentWindowHandle;

    它的第一个窗口。

  2. 一件重要的事情是:

    ReadOnlyCollection<string> windowHandles = driver.WindowHandles

    包含字符串名称对象,而不是 Windows 标题名称,例如 CollectionwindowHandles可以包含:

    不是 Windows 标题名称,因为 {Menu},{PopUp}
    它包含{45e615b3-266f-4ae0-a508-e901f42a36d3},{c6010037-0be6-4842-8d38-7f37c2621e81}

于 2012-11-07T16:51:00.353 回答
0
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("window.open()");
String ventanaPrincipal = driver.CurrentWindowHandle;
List<string> listWindow = new List<string>(driver.WindowHandles);
driver.SwitchTo().Window(listWindow[1]);
driver.Navigate().GoToUrl("http:www.google.com");
IWebElement search = driver.FindElement(By.Name("q"));
search.SendKeys("RPA");
于 2019-09-30T16:43:35.753 回答
0
        IWebDriver popup = null;
        string mainWindow = driver.CurrentWindowHandle;
        bool foundPopupTitle = false;
        foreach (string handle in driver.WindowHandles)
        {
            popup = driver.SwitchTo().Window(handle);
            if (popup.Title.Contains(title))
            {
                foundPopupTitle = true;
                break;
            }
        }

        if (foundPopupTitle)
        {
            popup.Close();
        }
        //switch back to original window
        driver.SwitchTo().Window(mainWindow);
于 2015-08-20T18:00:07.087 回答
-1
string NewWindowHandle = string.Empty;
ReadOnlyCollection<string> windowHandles = driver.WindowHandles;
NewWindowHandle = windowHandles[windowHandles.Count - 1];
driver.SwitchTo().Window(NewWindowHandle);
于 2019-11-05T11:34:24.543 回答