tab - window handles in selenium

Selenium getting data from another tab instead of active one (2)

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

Can you tell if clicking on the link is opening a new tab or a new window? There is a difference between the link opening in a new window and it opening in a new tab.

Case 1:
In case there are multiple windows, driver.getWindowHandles() returns the list of the handles and hence the size of the list is > 1. Below code demonstrates switching between windows:

//Get the current window handle
String windowHandle = driver.getWindowHandle();

//Get the list of window handles
ArrayList tabs = new ArrayList (driver.getWindowHandles());
//Use the list of window handles to switch between windows

//Switch back to original window

Case 2:
In case there are multiple tabs in the same window, then there is only one window handle. Hence switching between window handles keeps the control in the same tab.
In this case using Ctrl + \t (Ctrl + Tab) to switch between tabs, has worked for me.

//Open a new tab using Ctrl + t
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
//Switch between tabs using Ctrl + \t
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");

Detailed sample code to switch between tabs in same browser window can be found here:

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:


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


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