1

We have a legacy remote system here that dont have webapi, webservice, ...
then we need to do integration by Selenium.
We need open multiple tabs from one master tab to do the extraction but when changing to desired tab and getting value by a css selector it get always the result from the fisrt tab.
Our system cant be opened on internet then i did the same with Google as an example, the same behaviour happens. Its a bug or my fault ? Someone can see whats is wrong?
Below is a simplified version without error check, its minimal code.
Thanks a lot.

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");
    elmQtdRes = driver.FindElement(By.CssSelector("div#resultStats"));
    string strNewStat = elmQtdRes.Text;
    Console.WriteLine("Stat for WEB:[" + strWebStat + "]"); // prints stat for web - OK
    Console.WriteLine("Stat for NEWS:[" + strNewStat + "]"); // prints stat for web - WRONG
}
newway
  • 647
  • 1
  • 13
  • 21
  • Could you instead open multiple browser windows (via multiple FF driver instances) and point them to the URL's extracted from the master tab? – Slava Asipenko Dec 23 '15 at 02:10
  • @Slava Asipenko When i do the request with url on href it dont give me the desired page but an error, I really need click on link, open a new window will make me get the loaded url on detail tab to open on new window taking the double of time but i wrote a lot of code dealing with tabs that was working and it will go to trash. I have hundred of thousand heavy detail pages to open every day that now i will have to open twice. Thanks a lot for help, i will try it, if know how make Selenium work with tabs again i will be grateful twice. – newway Dec 23 '15 at 02:44

1 Answers1

3

WebDriver will not automatically switch focus to a new window or tab, so we have to tell it when to make the switch.

To switch to the new window/tab, try:

driver.SwitchTo().Window(driver.WindowHandles.Last());

Then, when you are finished on the new tab, close it and switch back to the primary window:

driver.Close();
driver.SwitchTo().Window(driver.WindowHandles.First());

Then from here, you can continue your test.

I'll add a quick disclaimer that I do not have Visual Studio installed on the machine I'm using at the moment and haven't tested the code above. This should, however get you in the right direction.

EDIT: Looking at your code a second time, this example above would replace the SendKeys() call to attempt to switch tab focus:

IWebElement elmBdy = driver.FindElement(By.CssSelector("body"));
elmBdy.SendKeys(Keys.Control + "2");
tim-slifer
  • 1,068
  • 1
  • 9
  • 17
  • Thanks a lot! I just put driver.SwitchTo().Window(driver.WindowHandles.Last()); after elmBdy.SendKeys(Keys.Control + "2"); and it works. – newway Dec 23 '15 at 12:50
  • ... but i dont call driver.close() because it close window with all tabs. – newway Dec 23 '15 at 14:17