Switch Commands


We often find situations where we might have to switch our focus/webdriver control to another window, popup, message box or frame. The reason we might have to do this is because of the way WebDriver has been designed. In short suffice to say that there is almost a separate DOM associated with each of the types mentioned above. And since Selenium WebDriver has been designed to handle only one DOM at a time with the reference handle we create, we will have to switch the references to be able to do operations. Lets see some examples below.

Switch to a new Window (using title)

Here we are switching to a newly opened window

 Switch to a New Browser Tab

Here we are switching to browser tab (tab in chrome is equivalent to new window)

Switch to javascript alert pop up

Here we are switching to a pop up and clicking ‘ok’ on the pop up

Switch to frame(or iframe)

Here we are switching to an iframe using its ‘name’ attribute. Subsequently we are retrieving the ‘body’ tag inside the frame and printing its text to see that we indeed were able to switch to an iframe




Complete Cucumber Scenario

Step Definitions