2

Selenium driver.WindowHandles return just one handle ignoring tabs on Firefox.
Cant navigate between multiple tabs.
See code below.
I had this problem and the response solved it, but my question wasnt precise, then im asking again.
The response helped me but now im stuck on the problem of this question, a problem that another person had too here but without answers.
There is another having it even with separate window but in chrome here.

//-------------------------------------------------
        public static void testab()
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("https://www.google.com/ncr");
            IWebElement elmTxt = driver.FindElement(By.CssSelector("input#lst-ib"));
            elmTxt.SendKeys("google" + Keys.Enter);
            IWebElement elmQtdRes = driver.FindElement(By.CssSelector("div#resultStats"));
            string strWebStat = elmQtdRes.Text;
            IWebElement elmNewsLnk = driver.FindElement(By.CssSelector("a[href*='tbm=nws']"));
            elmNewsLnk.SendKeys(Keys.Control + Keys.Return);
            IWebElement elmBdy = driver.FindElement(By.CssSelector("body"));
            elmBdy.SendKeys(Keys.Control + "2");
            //-------------------------------------------------
            driver = switchLastOpenedTab(driver); // works
            elmQtdRes = driver.FindElement(By.CssSelector("div#resultStats"));
            string strNewStatV1 = elmQtdRes.Text;
            Console.WriteLine("Stat for NEWS(V1):[" + strNewStatV1 + "]");
            Console.WriteLine("Stat for WEB:[" + strWebStat + "]");
            //-------------------------------------------------
            driver = switchTabByIndex(driver, 2);  // throw an Exception
            elmQtdRes = driver.FindElement(By.CssSelector("div#resultStats"));
            string strNewStatV2 = elmQtdRes.Text;
            Console.WriteLine("Stat for NEWS(V2):[" + strNewStatV2 + "]");
        }
        //-------------------------------------------------
        public static IWebDriver switchTabByIndex(IWebDriver driver, int tabIndex){
            // it dont get a list of tabs just the windows container
            List<String> winTabs = new List<String>(driver.WindowHandles);
            int idx = tabIndex - 1;
            // because of comment above count here equal one
            if (idx >= winTabs.Count) {
                throw new Exception("This method dont work!!");
            }
            driver.SwitchTo().Window(winTabs[idx]);
            return driver;
        }
        //-------------------------------------------------
        public static IWebDriver switchLastOpenedTab(IWebDriver driver)
        {
            // it works but cant handle more than one new tab, cant navigate
            driver.SwitchTo().Window(driver.WindowHandles.Last());
            return driver;
        }
        //-------------------------------------------------
Community
  • 1
  • 1
newway
  • 647
  • 1
  • 13
  • 21

0 Answers0