Selenium webdriver window handles c# switchTo failed
Asked Answered
C

5

5

Here comes 2 windows pop out during the testing.

my code:

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;
    }  
}                

I want to switch to the window which url contains "Main". But when the test is running, it switches between two windows continuously and it doesn't stop.

I debug and found the foreach didn't break even when the boolean a is true.

How can I resolve this?

Culm answered 28/6, 2012 at 2:34 Comment(1)
What happens if you just break, without switchTo() a 2nd time, since you already have switched when assigning the boolean.Monitorial
C
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);
Czech answered 16/7, 2012 at 9:29 Comment(3)
how do you get the existingWindowHandle ?Tingaling
existingWindowHandle = driver.CurrentWindowHandle;Spitzer
@Michael Ayers how can i get handle to current window(existing Window which is opened manually) which is not opened by driver means i can't use driver.getWindowHandle();Asthenopia
K
3
  1. Using the original post code.

    string existingWindowHandle = driver.CurrentWindowHandle;

    Its the first window.

  2. One important thing is:

    ReadOnlyCollection<string> windowHandles = driver.WindowHandles

    Contains the string name object, not the Windows Title Name, for example Collection windowHandles could contains:

    Not Windows Title Name as {Menu},{PopUp}
    It contains: {45e615b3-266f-4ae0-a508-e901f42a36d3},{c6010037-0be6-4842-8d38-7f37c2621e81}

Kiefer answered 7/11, 2012 at 16:51 Comment(0)
R
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);
Rickeyricki answered 20/8, 2015 at 18:0 Comment(2)
Code-only answers on 3 year old posts help no one. At least provide an explanation (and really, it should be better than the existing ones)Futurity
Code-only answer works perfectly for me. Code is more eloquent than words to a developer. And this answer is better in that it works in a scenario of multiple pop-ups. The first answer works only for 1 popup.Albany
C
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");
Champignon answered 30/9, 2019 at 16:43 Comment(0)
M
-1
string NewWindowHandle = string.Empty;
ReadOnlyCollection<string> windowHandles = driver.WindowHandles;
NewWindowHandle = windowHandles[windowHandles.Count - 1];
driver.SwitchTo().Window(NewWindowHandle);
Montoya answered 5/11, 2019 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.