Search code examples
.netwatin

Access each open browser window using WatiN


I can't see a way to simply loop through each open browser window in WatiN, only attach by xyz, which attaches to the first instance.

Basically I have multiple windows with the same url so I need to get these windows and then check page content for each before I can continue using the correct one.

Is this possible or does WatiN only ever return the first window?


Solution

  • You can use for example IECollection

    Here is sample :

        [STAThread]
        private static void Main(string[] args)
        {                        
            IE _ie = new IE("http://google.com");
            IE _ie2 = new IE("http://google.com");
            IE _ie3 = new IE("http://google.com");
            IE _ie4 = new IE("http://google.com");
            Debug.WriteLine(IE.InternetExplorers().Count);
    
            IECollection ies = new IECollection();
            foreach (var browser in ies)
            {
                Debug.WriteLine(browser.Url.ToString());
                Debug.WriteLine(browser.hWnd);
                IE browser2 = IE.AttachTo<IE>(Find.By("hwnd", browser.hWnd.ToString()));
                Thread.Sleep(1000);
                browser2.BringToFront();
            }
        }