Navigate And Fetch Title Of All Tabs Using Selenium WebDriver

This is a frequent interview question asked to all levels of experience in Selenium WebDriver. If you face this question as a beginner then the interviewer wants to check your knowledge of handling multiple windows, switching to windows, and getting the title of a window. But if you face this question as an experienced Selenium professional then you also need to exhibit your knowledge of Stream APIs of Java.

We will see sample code with and without stream usage.

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

Selenium Java code to navigate and get the title of each tab

package General;

import java.util.Set;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class NavigateThroughTabsAndGetTitle {
	
	@Test
	public void navigateThroughTabsAndGetTitle()
	{
		// Launching browser
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		// Loading Google - Tab 1
		driver.get("https://www.google.com/");
		driver.switchTo().newWindow(WindowType.TAB);
		// Loading Facebook - Tab 2
		driver.get("https://www.facebook.com/");
		driver.switchTo().newWindow(WindowType.TAB);
		// Loading GMail - Tab 3
		driver.get("https://www.google.com/intl/en-GB/gmail/about/#");
		driver.switchTo().newWindow(WindowType.TAB);
		// Loading Amazon - Tab 4
		driver.get("https://www.amazon.com/");
		driver.switchTo().newWindow(WindowType.TAB);
		// Loading Xoom - Tab 5
		driver.get("https://www.xoom.com/");
		
		// Get all window handles
		Set alltabs = driver.getWindowHandles();
		// Navigating and getting title without stream APIs
		for(String tab : alltabs)
		{
			String title = driver.switchTo().window(tab).getTitle();
			System.out.println(title);
		}
		System.out.println("===================================");
		
		// Using stream API
		alltabs.forEach(tab -> {
			String title = driver.switchTo().window(tab).getTitle();
			System.out.println(title);
		});
		System.out.println("===================================");
		// Same as above just stream() is redundant
		alltabs.stream().forEach(tab -> {
			String title = driver.switchTo().window(tab).getTitle();
			System.out.println(title);
		});
		System.out.println("===================================");
		
		// Quitting
		driver.quit();
		
	}
}
Output
Google
Facebook – log in or sign up
Gmail - Email from Google
Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more
Send Money Online | Xoom, a PayPal Service
===================================
Google
Facebook – log in or sign up
Gmail - Email from Google
Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more
Send Money Online | Xoom, a PayPal Service
===================================
Google
Facebook – log in or sign up
Gmail - Email from Google
Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more
Send Money Online | Xoom, a PayPal Service
===================================

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

1 thought on “Navigate And Fetch Title Of All Tabs Using Selenium WebDriver

Leave a Reply

Your email address will not be published. Required fields are marked *