How To Open Tab And Switching Between Tabs In Selenium WebDriver

Earlier we have learnt about how to switching between windows In selenium webdriver software testing tool as described on THIS PAGE. and work with multiple IFrames In THIS POST. As all of us are used to work with multuple tabs, some peoples don't like to work with multiple windows. So main question Is -> How to open new tab In selenium webdriver software test and then how to switch between tabs in selenium and take required actions. Also you can close all tabs using Robot class as described In THIS POST.

How to open new tab in selenium

WebDriver software automation testing tool do not have any built In method using which we can open new tab. Normally we are using CTRL + t Keys to open new tab In Browser. We can do same thing In webdriver software test for opening new tab In selenium webdriver, Bellow given syntax will open new tab In your driver browser Instance.
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");

How to switch between tabs in chrome using selenium webdriver

How to switch between tabs in selenium? Answer : For switching between tabs of browser, We are using CTRL + Tab keys. Same way, bellow given syntax will switch between tabs and select the content of selected tab.

Switch between tabs in selenium Example

//Switching between tabs using CTRL + tab keys.
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
//Switch to current selected tab's content.
driver.switchTo().defaultContent();

I have created simple example on tab switching for your better understanding. It will Open new tab and then switch between them to perform different actions on both tab's pages.
package Testing_Pack;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Tabs {

 WebDriver driver;
 
 @BeforeTest
 public void setup() throws Exception {
  //Use chrome driver to switch between tabs in chrome using selenium webdriver
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.com/2014/04/calc.html");
 }
 
 @Test
 public void openTab() {
  //Open tab 2 using CTRL + t keys.
  driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
  //Open URL In 2nd tab.
  driver.get("http://only-testing-blog.blogspot.com/2014/05/form.html");
  
  //Call switchToTab() function to switch to 1st tab
  switchToTab();
  //perform required actions on tab 1.
  driver.findElement(By.xpath("//input[@id='6']")).click();
  driver.findElement(By.xpath("//input[@id='plus']"));
  driver.findElement(By.xpath("//input[@id='3']"));
  driver.findElement(By.xpath("//input[@id='equals']"));
  
  //Call switchToTab() function to switch to 2nd tab.
  switchToTab();
  //perform required actions on tab 2.
  driver.findElement(By.xpath("//input[@name='FirstName']")).sendKeys("hi");
  driver.findElement(By.xpath("//input[@name='LastName']")).sendKeys("test");
  
  //Call switchToTab() function to switch to 1st tab
  switchToTab();
  //perform required actions on tab 1.
  String str = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
  System.out.println("Sum result Is -> "+str);
 } 
 
 public void switchToTab() {
  //Switching between tabs using CTRL + tab keys.
  driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
  //Switch to current selected tab's content.
  driver.switchTo().defaultContent();  
 }
}

This way, We can use keyboard actions to switch between tabs in chrome or firefox using selenium webdriver software automation testing tool.

10 comments:

  1. Thanks! this post really helps a lot

    ReplyDelete
  2. Hi,

    //Switching between tabs using CTRL + tab keys. driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");

    Could u plz help me o this?
    what is this "cssSelector("body")? Means this locator is use for what?

    ReplyDelete
    Replies
    1. I think it's body's tag of html.

      Delete
    2. Hi, the above command doesn't work. also cssSelector("body") why we are using.

      Delete
  3. Hi Arvind,

    above program illustrates how to switch between 2 tabs, if we have to switch between 3 tabs then how it can be handled. ?? please reply..

    ReplyDelete
  4. Hi Arvind,

    Above program illustrates how to swtich between 2 tabs , suppose if we have 3 tabs then how do we handle it?
    please reply

    ReplyDelete
  5. driver.get("http://only-testing-blog.blogspot.in/2014/05/form.html");

    http://only-testing-blog.blogspot.in/2014/05/form.html ............after opening the second tab this site also opens in the first tab and hence xpath("//input[@id='6'] is not tracable and it gives org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == //input[@id='6'] (WARNING: The server did

    ReplyDelete
  6. the given code is working when the class is single, if we used inheritance from other class file the it showing error

    Ex: i have defined one class and it have driverload function and in ur code i used that so it showing error
    Cannot instantiate class ****classname***

    ReplyDelete
  7. Hi Arvind,

    I have successfully built and run the code locally, but unable to open and switch between the tabs. Please guide me through this!

    ReplyDelete