Switch tabs using Selenium WebDriver with Java

341,979

Solution 1

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

This code perfectly worked for me. Try it out. You always need to switch your driver to new tab, before you want to do something on new tab.

Solution 2

This is a simple solution for opening a new tab, changing focus to it, closing the tab and return focus to the old/original tab:

@Test
public void testTabs() {
    driver.get("https://business.twitter.com/start-advertising");
    assertStartAdvertising();

    // considering that there is only one tab opened in that point.
    String oldTab = driver.getWindowHandle();
    driver.findElement(By.linkText("Twitter Advertising Blog")).click();
    ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
    newTab.remove(oldTab);
    // change focus to new tab
    driver.switchTo().window(newTab.get(0));
    assertAdvertisingBlog();

    // Do what you want here, you are in the new tab

    driver.close();
    // change focus back to old tab
    driver.switchTo().window(oldTab);
    assertStartAdvertising();

    // Do what you want here, you are in the old tab
}

private void assertStartAdvertising() {
    assertEquals("Start Advertising | Twitter for Business", driver.getTitle());
}

private void assertAdvertisingBlog() {
    assertEquals("Twitter Advertising", driver.getTitle());
}

Solution 3

There is a difference how web driver handles different windows and how it handles different tabs.

Case 1:
In case there are multiple windows, then the following code can help:

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

//Get the list of window handles
ArrayList tabs = new ArrayList (driver.getWindowHandles());
System.out.println(tabs.size());
//Use the list of window handles to switch between windows
driver.switchTo().window(tabs.get(0));

//Switch back to original window
driver.switchTo().window(mainWindowHandle);


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 is more useful.

//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 can be found here:
http://design-interviews.blogspot.com/2014/11/switching-between-tabs-in-same-browser-window.html

Solution 4

Work around

Assumption : By Clicking something on your web page leads to open a new tab.

Use below logic to switch to second tab.

new Actions(driver).sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(driver.findElement(By.tagName("html")),Keys.NUMPAD2).build().perform();

In the same manner you can switch back to first tab again.

new Actions(driver).sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(driver.findElement(By.tagName("html")),Keys.NUMPAD1).build().perform();

Solution 5

Since the driver.window_handles is not in order , a better solution is this.

first switch to the first tab using the shortcut Control + X to switch to the 'x' th tab in the browser window .

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "1");
# goes to 1st tab

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "4");
# goes to 4th tab if its exists or goes to last tab.
Share:
341,979
Umesh Kumar
Author by

Umesh Kumar

10 years of experience in extensive Automation(Web, Mobile, API) Testing

Updated on July 05, 2022

Comments

  • Umesh Kumar
    Umesh Kumar almost 2 years

    Using Selenium WebDriver with Java. I am trying to automate a functionality where I have to open a new tab do some operations there and come back to previous tab (Parent). I used switch handle but it's not working. And one strange thing the two tabs are having same window handle due to which I am not able to switch between tabs.

    However when I am trying with different Firefox windows it works, but for tab it's not working.

    How can I switch tabs? Or, how can I switch tabs without using window handle as window handle is same of both tabs in my case?

    (I have observed that when you open different tabs in same window, window handle remains same)

  • JimEvans
    JimEvans about 11 years
    Be very, very careful when indexing into the collection of window handles. There is no guarantee that these handles are presented in any order, and the first window opened may have the last window handle in the list returned by the getWindowhandles method (or its equivalent in your language).
  • Jury
    Jury about 8 years
    Works only on Chrome
  • BClaydon
    BClaydon almost 8 years
    YES. This is the first response the indicates there is only a single window handle and that is not the same as a tab. I don't know how the other solutions even work. How does getWindowHandles get the list of tabs for them? It only gets window handles for me in IE (I know, IE, blech)
  • Lucy
    Lucy over 7 years
    I'm using chrome driver and in that when I use the line "driver.switchTo().window(newTab.get(0));" a new window is opened along with a new tab...Any idea why this is happening?
  • Jordan Silva
    Jordan Silva about 7 years
    I haven't checked it, but I guess it is browser configuration.
  • Alex78191
    Alex78191 almost 7 years
    Maybe between multiple tabs?
  • Alex78191
    Alex78191 almost 7 years
    Tabs aren't windows. I guess it's not working in Firefox.
  • The Gilbert Arenas Dagger
    The Gilbert Arenas Dagger about 6 years
    This answer assumes order in driver.getWindowHandles(). That method returns a Set and therefore guarantees no order at the contract level. Perhaps your WebDriver implementatation is returning a Set implementation that does guarantee order, but why take the risk?
  • SourMonk
    SourMonk about 6 years
    Using ChromeDriver 2.33, getWindowHandles() does return a LinkedHashSet, so accessing by index works well. Note that you need to use getAt() instead of get()
  • sɐunıɔןɐqɐp
    sɐunıɔןɐqɐp almost 6 years
    Welcome to Stack Overflow! Please don't just throw your source code here. Be nice and try to give a nice description to your answer, so that others will like it and upvote it. See: How do I write a good answer?
  • Dada
    Dada over 2 years
    Could you please add some explanations to your answer, some reference to a documentation, etc, etc?
  • Admin
    Admin about 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.