Friday, 6 January 2017

How to Open New Tab with Selenium WebDriver

Selenium WebDriver  tool do not have any built In API using which we can directly use to open new tab. Normally we are using CTRL + t Keys to open new tab In Browser and cltr + tab to switch between the tabs. We will use this in our selenium test case to open another tab inside a browser and switch to tab. 

Check out the Example below in details to understand how to open a new tab, how to open particular url in the new tab, how to switch between two tabs  and how to close the tabs.

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class BromiumTests {

 WebDriver driver;
 Utils util;

 public void setUp() {
  System.setProperty("", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

 public void testOpenYoutube() {
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

 public void testOpenGmail() {
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
       public void openNewTab() {
  ((JavascriptExecutor) driver).executeScript("'about:blank','_blank');");

 public void switchToNewTab() {
  String subWindowHandler = null;

  Set<String> handles = driver.getWindowHandles();
  Iterator<String> iterator = handles.iterator();
  while (iterator.hasNext()) {
   subWindowHandler =;

 public void tearDown() {



  1. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.

    rpa Training in Bangalore

  2. Nice Article, Please do post this kind of posts more, thanks.
    I wish you might like to have a look at my article

  3. This comment has been removed by the author.

  4. UiPath Training in Bangalore by myTectra is one the best UiPath Training. myTectra is the market leader in providing Robotic Process Automation on UiPath
    ui path training in bangalore

  5. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Sql&plsql Online Training

    workday Online Training

    SAP GTS Online Training

    Oracle BPM Online Training

    Oracle SCM Online Training

  6. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Splunk Classes

    Tableau Classes

  7. This is good piece of writing and pleasant urging commented
    here, I am really enjoying by these.

  8. Hey, very nice site. I came across this on Google, and I am stoked that I did. I will definitely be coming back here more often. Wish I could add to the conversation and bring a bit more to the table, but am just taking in as much info as I can at the moment. Thanks for sharing.

    Custom Web Application Development

  9. Do not think! Just come with us to BGAOC and play with us. best casino games come to us and win soon.

  10. buy soma pill 350mg
    Soma pill 350mg has carisoprodol that effectively treats acute pain in people. Pain is felt by almost everyone at some point of their life. This can at times make the life of the person very difficult. Soma pill 350mg has helped people all over the world. So use soma pill 350mg and get rid of all your body pain. Buy soma pill online and have a healthy pain free life.

  11. BGAOC is the best online casino I've played. playing casino games Win as no one wins.

  12. Супер отличная гибкая світлодіодна стрічка на любой вкус и цвет, обычно покупаю в интернет магазине.

  13. No.1 Astrologer +91-9898765059 Best Jyotish Ahmedabad Astrologer Mahendra Joshi


    Website :-