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:

  1. Switch between two tabs using switchTo()
  2. 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.

 

Example of Switch between two tabs using switchTo():

 

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.

 

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.

 

Example of Switch between two tabs using Actions: 

 

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:

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