Waits and Handling

Alerts, Frames, Windows

Use this slide if there is no Heading
Note - Create Content inside Red Layout

[Delete Red Outline After creating slide]

Learning Outcome

5

Handle file uploads and complex UI interactions effectively

4

Learn how to manage multiple browser windows or tabs

3

Understand how to switch between frames and nested frames

2

Learn how to handle alerts in web applications

1

Understand the concept of context switching in automation

Topic Name-Recall(Slide3)

Hook/Story/Analogy(Slide 4)

Transition from Analogy to Technical Concept(Slide 5)

Working with Alerts: Accept, Dismiss, Read

 

Common Alert Methods

  • driver.switchTo().alert().accept();

  • driver.switchTo().alert().dismiss();

  • driver.switchTo().alert().getText();

When To Use

  • Confirmation dialogs

  • Warning messages

  • Input prompts

Scenario: Deleting a Record with Confirmation Alert

Handle Alert

Read the alert text and accept the confirmation.

Click Delete Button

Find the delete icon and trigger the action.

Verify Deletion

Confirm the record has been removed from the list.

Frames: Independent Web Pages Inside a Page

 

What Is an iFrame?

An HTML element that embeds another HTML document within the current page.

The Challenge

Selenium cannot access elements inside frames without explicitly switching context

Common Uses

Embedded forms, media players, maps, and third-party content.

3Ways to Switch to a Frame

ByWebElement

driver.switchTo().frame(frameElement);

​Return to Main Page

driver.switchTo().defaultContent();

By Name or ID

driver.switchTo().frame("frameId");

By Index

driver.switchTo().frame(0);

Frames: Independent Web Pages Inside a Page

Switch to Outer Frame driver.switchTo().frame("outerFrame");

Switch to Inner Frame driver.switchTo().frame("innerFrame");

Interact with Form driver.findElement(By.id("email")).sendKeys("test@example.com ")

Return to Main Page driver.switchTo().defaultContent();

Switching BetweenWindows Like a Pro

Current Window

  • String mainWindow = driver.getWindowHand le();

Switch Windows

  • driver.switchTo().windo w(windowId);

All Open Windows

  • Set allWindows = driver.getWindowHand les();

Return to Original

  • driver.switchTo().windo w(mainWindow);

Scenario: Handling File Uploads via Windows

The Challenge OS

file dialogs cannot be controlled by Selenium directly

The Solution

Use sendKeys() to set the file path on file input elements

Implementation

WebElement upload =driver.findElement(By.id("uploadFile")); upload.sendKeys("C:\\Users\\file.txt");

Switch Smartly: KnowYour Context

Context Type

Switch Method

Reset Method

Auto-reset after action

Alerts

switchTo().alert()

Frames

switchTo().defaultC ontent()

switchTo().window( originalHandle)

 switchTo().window( )

Windows

switchTo().frame()

Summary

5

After interaction, it is important to return to the original context

4

Multiple browser windows require switching using window handles

3

Frames are small sections in a page, so you must switch to use them

2

Alerts require switching to the alert before accepting or dismissing it

1

Selenium sometimes needs to switch context to interact with certain elements

Quiz

Which method returns to the main page after switching to a frame?

A. switchTo().main()

B. switchTo().defaultContent()

C. switchTo().parentPage()

D. switchTo().home()

Quiz-Answer

Which method returns to the main page after switching to a frame?

A. switchTo().main()

B. switchTo().defaultContent()

C. switchTo().parentPage()

D. switchTo().home()

Alerts, Frames, Windows

By Content ITV

Alerts, Frames, Windows

  • 9