2

I have a scenario like when I click on a link it opened in new tab. Using Selenium WebDriver how can we handle it.

As per my knowledge we can't switch to new tab but when I search in Web, got some below solutions.

ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
driver.close();
driver.switchTo().window(tabs2.get(0));

Unfortunately, every given solution contains driver.getWindowhandles(). But AFAIK even when a browser has multiple tabs it always returns only one handle.

My scenario is, when I click on one button it opens in new tab.Could any one please provide some solution to

  1. Switch between Tabs or
  2. How to open that tab in new window.
naveenz
  • 51
  • 1
  • 6
  • possible duplicate of [Clicking links in newly opened tab using WebDriver](http://stackoverflow.com/questions/18526868/clicking-links-in-newly-opened-tab-using-webdriver) – Mark Rowlands Jun 20 '14 at 15:01
  • Thanks Mark, But 'driver.getWindowhandles()' method giving only one handle when my browser has multiple tabs. This method working fine when I have multiple child windows. – naveenz Jun 23 '14 at 06:45
  • Possible duplicate of [How to open a new tab using Selenium WebDriver?](https://stackoverflow.com/questions/17547473/how-to-open-a-new-tab-using-selenium-webdriver) – Mate Mrše Mar 19 '19 at 12:49

2 Answers2

1

When your new tab has opened,then after that you are in any certain tab of the window.Now, you can use keys.chord(keys.ctrl,keys.tab) for switching between tabs. By using keys, we can take the keyboard i/p.

Alpha
  • 13,320
  • 27
  • 96
  • 163
debarghya
  • 11
  • 3
0

Write a method to switch the handle of a driver to a new window/tab based on the windows title:

public void SwitchHandleToNewWindow(IWebdriver driver, string windowTitle)
{
    ReadOnlyCollection<string> handles = driver.WindowHandles;
    foreach(string handle in handles)
    {
       driver.SwitchTo().Window(handle);
       if(driver.Title.Contains(windowTitle))
       {
           return;
       }
     }
  }

The code is straight forward, so implementation is straightforward too. If you want to switch to a new tab then you do something like : SwitchHandleToNewWindow(driver,"Test Page")