Window Tab Handle Using Selenium:
- Tab is Similar to Window.
- There is no difference with regards to Selenium.
- New tab content is the same as automating the window.
- WebDriver Software Automation Testing Tool does not have any built-in method that we can use to open new tabs.
- We can do the same thing to open a new tab in the Selenium WebDriver.
Ways to Window Tab Handle Using Selenium:
- Switch between two tabs using switchTo()
- Switch between two tabs using sendKeys Actions
1. Switch between two tabs using switchTo():
Scenarios of Below Code:
- Store all currently open windows into ArrayList.
- 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.
1 2 3 4 5 6 7 8 9 10 11 | // Store all currently open tabs in tabs ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles()); // Switch newly open Tab driver.switchTo().window(tabs.get(1)); // Close newly open tab after performing some operations. driver.close(); // Switch to old(Parent) tab. driver.switchTo().window(tabs.get(0)); |
Example of Switch between two tabs using switchTo():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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.firefox.FirefoxDriver; public class Tab_Handle_SwitchTo { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Store all currently open tabs in tabs ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles()); // Click on link to open in new tab driver.findElement(By.id("Url_Link")).click(); // Switch newly open Tab driver.switchTo().window(tabs.get(1)); // Perform some operation on Newly open tab // Close newly open tab after performing some operations. driver.close(); // Switch to old(Parent) tab. driver.switchTo().window(tabs.get(0)); } } |
2. Switch between two tabs using sendKeys Action Class:
Navigating left to right Tab using Action Class:
- Create an Instance / Object of Action Class.
- Use keyDown method from Action Class to Press CONTROL key from KeyBoard.
- Then send TAB keyboard key using sendKeys.
- Then use .build().perform() to perform switch Tab action.
1 2 3 | Actions action= new Actions(driver); action.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform(); |
Navigating right to left Tab using Action Class:
- Create an Instance / Object of Action Class to switch between Tab.
- Use keyDown method from Action Class to Press CONTROL key from KeyBoard.
- Then send TAB keyboard key using sendKeys to switch Tab.
- Then use .build().perform() to perform switch Tab action.
1 2 3 | Actions action= new Actions(driver); action.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).build().perform(); |
Example of Switch between two tabs using Actions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class Tab_Handle_Actions { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); Actions action = new Actions(driver); // Switch to Second Tab action.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform(); // Switch to First Tab action.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).build().perform(); } } |
Scenarios of Below Code:
- Set geckodriver path.
- Launch Broswser.
- Store current Tab location into string using getWindowHandle.
- Store All existing Tab’s location into string using getWindowHandles.
- Using For Each loop store all Tabs into string.
- Verify Current Tab with all existing Tabs.
- If Current Tab match between existing tab then Skip this step.
- If not match found then SwitchTo another Tab.
Example of Switch between two tabs using For Each Loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Tab_Handle_ForEach { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); String currentTab = driver.getWindowHandle(); for (String tab : driver.getWindowHandles()) { if (!tab.equals(currentTab)) { driver.switchTo().window(tab); // Perform operation on new Tab } } } } |
Switch tabs using Selenium WebDriver with Java and How can we switch to new opened tab with webdriver and How to switch to another Tab using Selenium WebDriver with Java