Selenium WebDriver provides several methods to switch between tabs in a web browser. This article will cover different approaches to handle tab switching in Selenium WebDriver using Java.
- Switching to a New Tab Using Window Handles
When a new tab is opened, it is assigned a unique window handle. You can switch to this new tab using the getWindowHandles() method and the switchTo().window() method.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
public class SwitchTabExample {
public static void main(String[] args) throws InterruptedException {
System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
WebDriver driver = new ChromeDriver();
// Open a website
driver.get(“https://example.com”);
// Open a new tab
driver.executeScript(“window.open(‘https://google.com’, ‘_blank’);”);
Thread.sleep(2000); // Wait for the new tab to open
// Get the window handles
Set<String> handles = driver.getWindowHandles();
String currentHandle = driver.getWindowHandle();
// Switch to the new tab
for (String handle : handles) {
if (!handle.equals(currentHandle)) {
driver.switchTo().window(handle);
break;
}
}
System.out.println(driver.getTitle()); // Should print the title of the new tab
// Close the new tab
driver.close();
// Switch back to the original tab
driver.switchTo().window(currentHandle);
System.out.println(driver.getTitle()); // Should print the title of the original tab
// Close the driver
driver.quit();
}
}
- Using Keyboard Shortcuts to Switch Tabs
You can simulate keyboard shortcuts to switch tabs using the Robot class in Java.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class SwitchTabWithRobot {
public static void main(String[] args) throws Exception {
System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
WebDriver driver = new ChromeDriver();
// Open a website
driver.get(“https://example.com”);
// Open a new tab
driver.executeScript(“window.open(‘https://google.com’, ‘_blank’);”);
Thread.sleep(2000); // Wait for the new tab to open
// Use Robot to switch tabs
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_CONTROL);
Thread.sleep(2000); // Wait for the tab switch
System.out.println(driver.getTitle()); // Should print the title of the new tab
// Close the driver
driver.quit();
}
}
- Switching Tabs by Title
If you know the title of the tab you want to switch to, you can loop through the window handles and switch to the tab with the matching title.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
public class SwitchTabByTitle {
public static void main(String[] args) throws InterruptedException {
System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
WebDriver driver = new ChromeDriver();
// Open a website
driver.get(“https://example.com”);
// Open a new tab
driver.executeScript(“window.open(‘https://google.com’, ‘_blank’);”);
Thread.sleep(2000); // Wait for the new tab to open
// Function to switch to a tab by title
switchToTabByTitle(driver, “Google”);
// Close the driver
driver.quit();
}
public static void switchToTabByTitle(WebDriver driver, String title) {
Set<String> handles = driver.getWindowHandles();
for (String handle : handles) {
driver.switchTo().window(handle);
if (driver.getTitle().equals(title)) {
System.out.println(“Switched to the ” + title + ” tab.”);
return;
}
}
System.out.println(“Tab not found.”);
}
}
- Closing Tabs
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
public class CloseTabExample {
public static void main(String[] args) throws InterruptedException {
// Set the path for the ChromeDriver
System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
WebDriver driver = new ChromeDriver();
// Open a website
driver.get(“https://example.com”);
// Open a new tab
driver.executeScript(“window.open(‘https://google.com’, ‘_blank’);”);
Thread.sleep(2000); // Wait for the new tab to open
// Get the window handles
Set<String> handles = driver.getWindowHandles();
String originalTab = driver.getWindowHandle(); // Store the original tab handle
// Switch to the new tab
for (String handle : handles) {
if (!handle.equals(originalTab)) {
driver.switchTo().window(handle);
break;
}
}
// Print the title of the new tab
System.out.println(“New Tab Title: ” + driver.getTitle());
// Close the new tab
driver.close();
// Switch back to the original tab
driver.switchTo().window(originalTab);
System.out.println(“Original Tab Title: ” + driver.getTitle()); // Should print the title of the original tab
// Close the driver
driver.quit();
}
}
Conclusion
Switching tabs in Selenium WebDriver can be accomplished using various methods, including using window handles, keyboard shortcuts, and tab titles. Understanding these methods will enhance your ability to automate complex web applications effectively. Always remember to manage your tabs and windows properly to avoid memory leaks and ensure smooth test execution.