Handle Multiple Browser Tabs with Selenium Webdriver

In this tutorial, we will learn to handle multiple browser tabs within a single Selenium script. Selenium Webdriver does not provide any commands for this task. But we can make use of the existing Selenium commands in a smarter way to automate this scenario. Although this scenario is quite rare and you will not encounter it usually.

How to use Actions Class in Selenium to Handle Multiple Browser Tabs

Actions Class in Selenium is a special class which provides us very useful commands, which helps us to replicate Keyboard and Mouse events i.e., press/release any Key, move curson, drag and drop, right click, double click etc. You can learn about Actions Class in detail here

In our scenario, we'll make use of Key control commands of Actions class to automate open and automate multiple browsers. Let's understand the whole code logic step by step.

1. Launch google.com with Selenium Webdriver

This is the most common part, we'll launch google.com with Selenium Webdriver as usual.

// Set Driver path
System.setProperty("webdriver.chrome.driver", "C:\\AUTOMATION\\chromedriver.exe");

// Initialize driver  
WebDriver driver=new ChromeDriver();

//Maximize browser window   
driver.manage().window().maximize();  

//Go to URL  
driver.get("http://www.google.com");  

//Set timeout  
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  

2. Automate Opening of two browser tabs

Now our first browser tab is opened and google.com is launched inside it. So, step 2 is to open our second browser tab. Like we already know that manually we can open a new tab in a browser by pressing 'ctrl' key and 't' key together. We are going to automate of this shortcut using Selenium Actions Class.

// Open new tab by pressing ctrl + t
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");  

//Go to URL  
driver.get("http://www.gmail.com");  

//Set new tab timeout  
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  

// Do some operations on gmail like login      

3. Automate Switching between two tabs during execution of automation script

We can switch between different tabs on a browser by pressing 'ctrl' and 'tab'. We'll automate same with Actions class.

// Switch back to first tab (google.com) by pressing ctrl + tab
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t");  
driver.switchTo().defaultContent();  
Thread.sleep(2000);  

// Do some operation on google.com

Important - We can also use Robot Class of Java to handle multiple tabs.

Complete Sample Program to Handle two Browser tabs:

public class SwitchBetweenBrowserTab {  

 public static void main(String[] a) throws InterruptedException {  
     
      // Set Driver path
      System.setProperty("webdriver.chrome.driver", "C:\\AUTOMATION\\geckodriver.exe");
// Initialize driver WebDriver driver = new FirefoxDriver(); //Maximize browser window driver.manage().window().maximize(); //Go to URL driver.get("http://www.google.com"); //Set timeout driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Open new tab driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t"); //Go to URL driver.get("http://www.gmail.com"); //Set new tab timeout driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Do some operation driver.findElement(By.id("gmail-sign-in")).click(); driver.findElement(By.id("Email")).sendKeys("WebDriver"); driver.findElement(By.id("Passwd")).sendKeys("WebDriver"); driver.findElement(By.id("signIn")).click(); Thread.sleep(2000); // Switch first tab driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t"); driver.switchTo().defaultContent(); Thread.sleep(2000); // Write search String driver.findElement(By.id("gbqfq")).sendKeys("WebDriver"); // Click on Search button driver.findElement(By.id("gbqfb")).click(); Thread.sleep(2000); // Browser close driver.close(); } }

Program Explanation:
  • Open Google.com
  • Then open a new tab by making use of Key class. By combination of CTRL + t key a new browser tab will be opened.
  • Open Gmail.com in that tab.
  • Perform some actions on gmail sign in page.
  • Switch back to google.com tab by pressing Ctrl + \t
  • Now enter some keywords in google search box
  • Close the browsers.

Handle Alerts or Pop-ups  << Previous   ||   Next >>   Verify Broken Links on a Webpage

Happy Learning!

Follow Us

Quora Space | Facebook Page | Telegram Channel
Feel free to ask queries or share your thoughts in comments or email us.

Comments

Popular posts from this blog

10 Best Demo Websites for Selenium Automation Testing Practice

Automate an e-Commerce website with Selenium Webdriver

How to Handle Dynamic Web Table in Selenium WebDriver

Top 50 Selenium Webdriver Interview Questions

Automate Browser Actions and Menu links with Selenium Webdriver

Top 25 Selenium Webdriver Commands for Test Automation