4

I know this is a contradictory topic. "Officially" the driver does not support tabs, however many places state that newly opened tabs will be available via window handles, and we can use the handles to switch between tabs. (code samples are C# but I hope there is nothing special to C# in the question)

 driver.SwitchTo.Window(myHandle);

I am trying to open a link in a new tab. I have success, the browser shows the new tab, however the driver's windowhandles does not contain the newly opened tab, only the one original window handle. This seems to be logical, the tab is not a window, however again, in many places it is described that it should work, and driver treats tabs as windows. What am I missing?

Open in new tab:

// Performing Ctrl + Click on my link:
new Actions(driver)
    .KeyDown(Keys.Control)
    .Click(myLink)
    .KeyUp(Keys.Control).Perform();

// driver.WindowHandles did **not** change, still contains one handle
// The newly opened tab can not be reached, because we can not even switch
// the driver to it.

Open in new window:

// Performing context menu and "Open new Window" on my link
new Actions(driver)
    .ContextClick(myLink)
    .SendKeys("w")
    .Perform();
// driver.WindowHandles **changed**, contains 2 handles
// Switch to the newly opened window works:
driver.SwitchTo().Window(driver.WindowHandles.Last());

Misc information:

  • Using Firefox v43.0.4
  • Using Official Selenium C# bindings v2.48.2 (nuget)
  • OS Windows 7 64 bit
  • One of the zillon places where tabs described as working: here (see all answer and comments too)
Community
  • 1
  • 1
g.pickardou
  • 32,346
  • 36
  • 123
  • 268

1 Answers1

1

There is a difference between the browsers, for example in Chrome the driver does recognize two window handles. In FireFox I also have only one window handle but the focus is on the new tab.

To switch between the tabs you can use Actions

action.KeyDown(Keys.Control).SendKeys("2").Perform(); //to switch to the second tab
Guy
  • 46,488
  • 10
  • 44
  • 88
  • Will the driver "drive" the 2nd tab after switched to it via SendKeys? – g.pickardou Jan 11 '16 at 09:50
  • Thanks for the answer. I will test it. However I think in my case this switch facility will not robust enough. I intend to open / close up to 50 tabs parallel. Sooner or later the C# programs internal represantation which tab is which index will synced out with the browser... – g.pickardou Jan 11 '16 at 10:10
  • In case of IE 11 it doesn't switch to new tab actually even though visible focus changed. – Mardok Feb 28 '17 at 14:11