naveenz
naveenz

Reputation: 101

How can we switch to new opened tab with webdriver

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.

Upvotes: 2

Views: 3464

Answers (2)

debarghya
debarghya

Reputation: 11

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.

Upvotes: 1

twisted_coder
twisted_coder

Reputation: 124

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

Upvotes: 0

Related Questions