Skip to main content

Quality Assurance

Comparing the Different Selenium Switching Techniques

Man working on a computer with lines of code superimposed over him.

Before we go any further, let’s understand why and when the switch method is used for. Frames, notifications, and windows can be switched using Selenium Switch Methods. Before performing an action inside the frame, alert, or window, our Test Script needs to switch. If we don’t switch, an exception is raised by the application. switchTo() provides access to the different Selenium switch methods. In order to transmit a future command, switchTo() returns a target location.

Switch To Frame

There are 4 ways to choose a frame and only one way to go to the parent context:

  • Using a WebElement
  • Using a name or ID:
    • driver.switchTo().frame("iframe");
  • Using an index:
    • driver.switchTo().frame(0);
  • Leaving a frame

Switch To Alert

A pop-up box is an alert notifying the user or requesting a certain action. There are 3 different sorts of notifications and 4 different things you may do with an alert. The 3 different alert kinds are:

  • Information Alert: Contains 1 button and a message.
  • Confirmation Alert: This alert has a message and two buttons.
  • Prompt Alert: The prompt alert message has two buttons and a text entry box.

An alarm is typically displayed with a grey disabled backdrop. The website is grey because it cannot be used to perform an action. The alarm needs to be handled in some way.

The test script that follows switches to the alert and accepts it if the pop-up image was an alert.


public void demoSwitchToAlert () {




Switch To Window

All windows have a distinct alphanumeric id that Selenium assigns. The id is used to switch control between each window and is referred to as a window handle. There are two techniques for obtaining the window handle:

  • Get the current window handle with getWindowHandle().
  • Get a list of window handles with getWindowHandles().

The following statement lines get the window handle and window handles:

  • String mainHandle = driver.getWindowHandle(): gets the HomePage window handle.
  • Set <String> allHandles = driver.getWindowHandles(): gets both window handles.

Happy Testing! 😎

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Mangesh Sonwane

Mangesh Sonwane is an Associate Technical Consultant at Perficient in Nagpur GDC. He has an experience of 1+ years in Drupal and AEM. Apart from this passionate about learning new changes and expanding his knowledge in Automation. He is very committed to his work and is always ready to face any challenging projects.

More from this Author

Follow Us