Waits and Handling

 

Alerts, Frames, Windows

Learning Outcome

4

Perform basic context switching for web elements

3

Handle JavaScript alerts using accept and dismiss actions

2

Switch between main page, frames, and windows

1

Understand alerts, frames, and multiple browser windows

5

Improve test execution in multi-context web pages

 

Recall

Handling Alerts, Frames, Windows

 

Before handling the Alerts, Frames & Windows in Selenium WebDriver, we should recall these basic concepts:

 

Be familiar with basic WebDriver actions like click, sendKeys, and get

Understand HTML DOM structure and how web pages are built

Know locators like id, name, xpath, and cssSelector

Understand that Selenium works in a single active context at a time

Understand the concept of waits (implicit and explicit) for synchronization

Know that a browser can handle multiple windows and tabs

Think of a website as a large office building and Selenium as a worker inside it.

 1. Alerts = Emergency Interruption

 

You cannot ignore it
You m
ust either
   

  • Accept (OK) → acknowledge message

  • Dismiss (Cancel) → close it

 

An alert is like:

A sudden fire alarm or security message that pops up while you are working.

 

 Until you handle it, you cannot continue your work.

2. Frames = Rooms Inside the Same Office

 

A frame/iframe is like:

 

A separate room inside the same building.

 

You are still in the same office (same website)

But you are inside a different room

To work there, you must enter that room first

 You must “switch room” before interacting with objects inside.

 

3. Windows = Different Buildings

 

A new window/tab is like:

 

A completely different office building

Same company (application), but different location

You must:

 

  • Go out of current building

  • Enter new building

  • Work separately there



 

3. Windows = Different Buildings

 

In all cases, you must switch context before acting

 

 Each window has its own environment.

 

  • Alert → interrupt you

  • Frame → change room

  • Window → change building

Handling alerts, frames, and windows in Selenium is like dealing with interruptions, moving between rooms, and switching buildings in an office.

Why Handle Alerts, Frames & Windows in Selenium WebDriver?

 

Modern web apps are multi-context systems, but Selenium can work in only one context at a time.

That’s why handling alerts, frames, and windows is essential.

 

Ensures Selenium is always in the correct active context

Prevents failures caused by wrong page, frame, or blocked alert

Enables interaction with dynamic UI structures (pop-ups, iframes, tabs)

Maintains continuous and stable test flow without interruption

What is an Alert?

An Alert is a JavaScript-based modal pop-up dialog that appears on a web page to display messages, warnings, confirmations, or to collect input from the user.

It pauses the normal execution flow of the browser until the user or automation script handles it.

Alerts are browser-level pop-ups, not part of the HTML DOM

They are generated using JavaScript (window.alert, window.confirm, window.prompt)

Because they are outside DOM, Selenium cannot locate them using locators

Types of Alerts

Simple alert displays some information or warning on the screen

  • Shows message + OK button

  • Used for notifications

1.Simple Alert

2. Confirmation Alert

Confirmation alert asks permission to do some type of operation

  • Has OK and Cancel

  • Used for user decisions(delete, logout, etc.)

Types of Alerts

Prompt Alert asks some input from user selenium webdriver can enter the text using sendkeys(" input…. ")

  • Accepts user input

  • Has text box + OK/Cancel

3. Prompt Alert

How to Handle Alerts?

 

Alerts act as a blocking layer between user and browser execution flow, forcing explicit handling before continuing automation. To handle alert

 

import org.openqa.selenium.Alert;

Import this package prior to the script creation

This package references to the Alert Interface  which is required to handle the web based alerts

Alert alert=driver.switchTo().alert();

We create a reference variable for Alert Interface and references it to the alert. 

The above command is used to switch the control to the recently generated pop up window.

In Selenium WebDriver, alerts are handled using:

 

switchTo().alert()

to access alert

accept()

click OK

 dismiss()  

click Cancel

sendKeys()

enter text (for prompt alerts)

getText()

Get Alert message

 Selenium must explicitly switch context to the alert.

 

Handling Multiple Windows & Tabs

When we have multiple windows in any web application, the activity may need to switch control among several windows from one to other in order to complete the operation After completion of the operation, it has to return to the main window i.e. parent window.

To handle all opened windows by web driver, we can use

 

 “driver.getWindowHandles()"

 And then we can switch from one window to another in a web application .

 

 Its return type is Iterator

  Set<String> allWindows =  driver.getWindowHandles();

 

  driver.getWindowHandle()

Return to parent window

This will handle the current window that uniquely identifies it within this driver instance.

 

Its return type is String

  String parent = driver.getWindowHandle();

  driver.switchTo().window(parent);

When the site opens, we need to handle the main window by

What is Frame?

 

A frame/iframe is:

 

A web page inside another web page 

An embedded HTML document within the main DOM

Why Frame Handling is Needed?

 

By default, Selenium:

 

Works only in the main page context

Cannot see elements inside a frame

 So if you try without switching:

 

NoSuchElementException occurs

Handling Frames

 

Handling frames (iframes) means switching Selenium’s control from the main page to an embedded HTML document so that elements inside it can be accessed and interacted with.

 

To interact with frame elements, Selenium must switch context using the following ways.

 

By Index  

 

switchTo.frame(int frameNumber)    
 

Pass the frame index and driver will switch to that frame to switch to 0th iframe use

 

driver.switchTo().frame(0)

 

 By Name or ID  

 

switchTo.frame(string frameNameOrId)

 

Pass the frame ID or frame Name and driver will switch to that frame

 

driver.switchTo().frame(“iframe1”)

 

By WebElement  

switchTo.frame(WebElement frameElement)

 

 Pass the frame web element and driver will switch to that frame

 

WebElement iframeElement = driver.findElement(By.id("IF1"));

 

driver.switchTo().frame(iframeElement);

 

Switching Back to Main Page

 

driver.switchTo().defaultContent();

OR

driver.switchTo().parentFrame();

 

Summary

4

3

2

1

WebElements represent HTML elements on a web page in Selenium.

5

Check element states: displayed, enabled, selected.

 

Perform actions: click, type, clear, submit.

1

Alerts, frames, and windows need context switching.

2

Use accept(), dismiss(), sendKeys() for alerts.

3

Switch to frames to access inner elements.

4

Use window handles to switch tabs/windows.

5

WebElements enable verification of element states for testing.

 

Quiz

Which command is used to switch to a frame in Selenium?

A. switchTo().frame()

B.switchTo().alert()

C. switchTo().window()

D.getWindowHandles()

Quiz - Answer

Which command is used to switch to a frame in Selenium?

B.switchTo().alert()

C. switchTo().window()

D.getWindowHandles()

A. switchTo().frame()

Waits and Handling (Alerts, Frames, Windows)

By Content ITV

Waits and Handling (Alerts, Frames, Windows)

  • 20