Handle Browser Tabs In Selenium (Keyboard actions)

Browser Tabs
  • Tab is Similar to Window.
  • There is no difference when handle New tab content in Selenium.

There is two ways to handle browser tabs in Selenium :

  1. Switch between two tabs using sendKeys() Actions Class
  2. Switch between two tabs using switchTo()

Switch between two tabs using sendKeys() Actions Class

Scenario 1: Navigating left to right Tab

As we know we can switch tab manually by pressing “control + tab” keys

Selenium WebDriver Steps

  • Create an Object of Action Class.
  • Use keyDown method from Action Class to Press CONTROL key from Keyboard.
  • Then send TAB key using sendKeys.
  • Then use .build().perform() to perform switch Tab action.
Actions action= new Actions(driver); 
action.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform();

Scenario 2: Navigating right to left Tab using Action Class

As we know we can switch tab manually by pressing “control +Shift+ tab” keys

Selenium WebDriver Steps

  • Create an Object of Action Class.
  • Use keyDown method from Action Class to Press CONTROL key from Keyboard.
  • Use keyDown method from Action Class to Press SHIFT key from Keyboard.
  • Then send TAB key using sendKeys.
  • Then use .build().perform() to perform switch Tab action.
Actions action= new Actions(driver); 
action.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).build().perform();

Example

package automationTestingLab;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class TabSwitch {

	public static void main(String[] args) throws InterruptedException {

		System.setProperty("webdriver.chrome.driver","F:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.get("https://rahulshettyacademy.com/AutomationPractice/");

		WebElement newTab = driver.findElement(By.xpath("//a[@id='opentab']"));

		newTab.click();

		Actions action = new Actions(driver);
		// Switch to First Tab
		action.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).build().perform();

		Thread.sleep(3000);

		// Switch to Second Tab
		action.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform();

		driver.close();
	}
}

Switch between two tabs using switchTo()

Selenium WebDriver Steps

  • Store all currently open windows into Array List.
  • Then switch to Newly open Tab using switchTo().
  • If you perform action on Newly open tab & you don’t want to perform any operation on this Tab then close this Tab.
  • Switch back into main Tab.
package automationTestingLab;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class TabHandle_SwitchTo {

	public static void main(String[] args) throws InterruptedException {

		System.setProperty("webdriver.chrome.driver","F:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.get("https://rahulshettyacademy.com/AutomationPractice/");

		WebElement newTab = driver.findElement(By.xpath("//a[@id='opentab']"));

		// Store all currently open tabs in tabs
		ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());

		// Click on link to open in new tab
		newTab.click();

		// Switch newly open Tab
		driver.switchTo().window(tabs.get(0));

		Thread.sleep(3000);
	
		// Switch to old(Parent) tab.
		driver.switchTo().window(tabs.get(1));
		driver.close();

	}
}

Leave a comment

Design a site like this with WordPress.com
Get started