Selenium webdriver窗口处理c#switchTo失败

时间:2012-06-28 02:34:48

标签: c# selenium for-loop webdriver window-handles

测试期间会弹出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没有收支平衡。

我该如何解决这个问题?

5 个答案:

答案 0 :(得分:12)

//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);

答案 1 :(得分:3)

  1. 使用原始邮政编码。

    string existingWindowHandle = driver.CurrentWindowHandle;

    它是第一个窗口。

  2. 一件重要的事情是:

    ReadOnlyCollection<string> windowHandles = driver.WindowHandles

    包含字符串名称对象,而不是Windows标题名称 示例集合windowHandles可以包含:

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

答案 2 :(得分: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);

答案 3 :(得分: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");

答案 4 :(得分:-1)

string NewWindowHandle = string.Empty;
ReadOnlyCollection<string> windowHandles = driver.WindowHandles;
NewWindowHandle = windowHandles[windowHandles.Count - 1];
driver.SwitchTo().Window(NewWindowHandle);